├── App.config ├── Properties └── AssemblyInfo.cs ├── LICENSE ├── RecursiveDelete.sln ├── README.md ├── .gitattributes ├── RecursiveDelete.csproj ├── .gitignore └── Program.cs /App.config: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /Properties/AssemblyInfo.cs: -------------------------------------------------------------------------------- 1 | // Copyright © 2015, EPSITEC SA, CH-1400 Yverdon-les-Bains, Switzerland 2 | // Author: Pierre ARNAUD, Maintainer: Pierre ARNAUD 3 | 4 | using System.Reflection; 5 | using System.Runtime.InteropServices; 6 | 7 | [assembly: AssemblyTitle ("RecursiveDelete")] 8 | [assembly: AssemblyDescription ("")] 9 | [assembly: AssemblyConfiguration ("")] 10 | [assembly: AssemblyCompany ("Epsitec")] 11 | [assembly: AssemblyProduct ("RecursiveDelete")] 12 | [assembly: AssemblyCopyright ("Copyright © 2015, EPSITEC SA, CH-1400 Yverdon-les-Bains, Switzerland")] 13 | [assembly: AssemblyTrademark ("")] 14 | [assembly: AssemblyCulture ("")] 15 | 16 | [assembly: ComVisible (false)] 17 | 18 | [assembly: AssemblyVersion ("1.2.1537.0")] 19 | [assembly: AssemblyFileVersion ("1.2.1537.0")] 20 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Pierre Arnaud 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /RecursiveDelete.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 2013 4 | VisualStudioVersion = 12.0.31101.0 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RecursiveDelete", "RecursiveDelete.csproj", "{1F082D2E-A45C-43A0-B533-980E17769C4A}" 7 | EndProject 8 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Notes", "Notes", "{94C93C86-0E14-49DB-B1A2-29BA0D4E1757}" 9 | ProjectSection(SolutionItems) = preProject 10 | README.md = README.md 11 | EndProjectSection 12 | EndProject 13 | Global 14 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 15 | Debug|Any CPU = Debug|Any CPU 16 | Release|Any CPU = Release|Any CPU 17 | EndGlobalSection 18 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 19 | {1F082D2E-A45C-43A0-B533-980E17769C4A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 20 | {1F082D2E-A45C-43A0-B533-980E17769C4A}.Debug|Any CPU.Build.0 = Debug|Any CPU 21 | {1F082D2E-A45C-43A0-B533-980E17769C4A}.Release|Any CPU.ActiveCfg = Release|Any CPU 22 | {1F082D2E-A45C-43A0-B533-980E17769C4A}.Release|Any CPU.Build.0 = Release|Any CPU 23 | EndGlobalSection 24 | GlobalSection(SolutionProperties) = preSolution 25 | HideSolutionNode = FALSE 26 | EndGlobalSection 27 | EndGlobal 28 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Tools-RecursiveDelete 2 | 3 | This tool takes a single argument on the command line: 4 | 5 | ``` 6 | RecursiveDelete foo\bar\node_modules 7 | ``` 8 | 9 | and it will delete the specified folder and all of its contents. In order to 10 | walk around limitations in the Windows API, we rename the folders to `@` on 11 | the way down the tree in order to shorten the path names, so that we don't 12 | get errors because the path exceeds 250. 13 | 14 | ## What's the matter? 15 | 16 | When working with `npm` it is not unusal to get `node_modules` directory 17 | trees which are very deep, with paths reaching lengths of 400 or more 18 | characters (this is a [known issue](https://github.com/joyent/node/issues/6960)). 19 | 20 | Trying to delete these folders with the _Windows Explorer_ results in 21 | error messages complaining about the fact that the _path is too long_ and 22 | deletion becomes a nightmare. 23 | 24 | To circumvent this problem, I've written this tiny naive tool which just 25 | walks the folder structure, renames every folder to a one-character length 26 | name while descending the tree, and then deletes everything while moving 27 | back up the tree. 28 | 29 | ## I'm left with a folder called @ 30 | 31 | Sometimes, folders or files are in use (usually by the `explorer` process, 32 | but this might also be the antivirus kicking in) and the tool cannot delete 33 | or rename some folders or files. 34 | 35 | Usually, the resulting folder structure has already been shortened enough so 36 | that you can simply delete the remaining garbage with the _Windows Explorer_ 37 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | ############################################################################### 2 | # Set default behavior to automatically normalize line endings. 3 | ############################################################################### 4 | * text=auto 5 | 6 | ############################################################################### 7 | # Set default behavior for command prompt diff. 8 | # 9 | # This is need for earlier builds of msysgit that does not have it on by 10 | # default for csharp files. 11 | # Note: This is only used by command line 12 | ############################################################################### 13 | #*.cs diff=csharp 14 | 15 | ############################################################################### 16 | # Set the merge driver for project and solution files 17 | # 18 | # Merging from the command prompt will add diff markers to the files if there 19 | # are conflicts (Merging from VS is not affected by the settings below, in VS 20 | # the diff markers are never inserted). Diff markers may cause the following 21 | # file extensions to fail to load in VS. An alternative would be to treat 22 | # these files as binary and thus will always conflict and require user 23 | # intervention with every merge. To do so, just uncomment the entries below 24 | ############################################################################### 25 | #*.sln merge=binary 26 | #*.csproj merge=binary 27 | #*.vbproj merge=binary 28 | #*.vcxproj merge=binary 29 | #*.vcproj merge=binary 30 | #*.dbproj merge=binary 31 | #*.fsproj merge=binary 32 | #*.lsproj merge=binary 33 | #*.wixproj merge=binary 34 | #*.modelproj merge=binary 35 | #*.sqlproj merge=binary 36 | #*.wwaproj merge=binary 37 | 38 | ############################################################################### 39 | # behavior for image files 40 | # 41 | # image files are treated as binary by default. 42 | ############################################################################### 43 | #*.jpg binary 44 | #*.png binary 45 | #*.gif binary 46 | 47 | ############################################################################### 48 | # diff behavior for common document formats 49 | # 50 | # Convert binary document formats to text before diffing them. This feature 51 | # is only available from the command line. Turn it on by uncommenting the 52 | # entries below. 53 | ############################################################################### 54 | #*.doc diff=astextplain 55 | #*.DOC diff=astextplain 56 | #*.docx diff=astextplain 57 | #*.DOCX diff=astextplain 58 | #*.dot diff=astextplain 59 | #*.DOT diff=astextplain 60 | #*.pdf diff=astextplain 61 | #*.PDF diff=astextplain 62 | #*.rtf diff=astextplain 63 | #*.RTF diff=astextplain 64 | -------------------------------------------------------------------------------- /RecursiveDelete.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | Debug 6 | AnyCPU 7 | {1F082D2E-A45C-43A0-B533-980E17769C4A} 8 | Exe 9 | Properties 10 | RecursiveDelete 11 | RecursiveDelete 12 | v4.5.1 13 | 512 14 | true 15 | 16 | 17 | AnyCPU 18 | true 19 | full 20 | false 21 | bin\Debug\ 22 | DEBUG;TRACE 23 | prompt 24 | 4 25 | 26 | 27 | AnyCPU 28 | pdbonly 29 | true 30 | bin\Release\ 31 | TRACE 32 | prompt 33 | 4 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 59 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | # User-specific files 5 | *.suo 6 | *.user 7 | *.sln.docstates 8 | 9 | # Build results 10 | [Dd]ebug/ 11 | [Dd]ebugPublic/ 12 | [Rr]elease/ 13 | x64/ 14 | build/ 15 | bld/ 16 | [Bb]in/ 17 | [Oo]bj/ 18 | 19 | # Roslyn cache directories 20 | *.ide/ 21 | 22 | # MSTest test Results 23 | [Tt]est[Rr]esult*/ 24 | [Bb]uild[Ll]og.* 25 | 26 | #NUNIT 27 | *.VisualState.xml 28 | TestResult.xml 29 | 30 | # Build Results of an ATL Project 31 | [Dd]ebugPS/ 32 | [Rr]eleasePS/ 33 | dlldata.c 34 | 35 | *_i.c 36 | *_p.c 37 | *_i.h 38 | *.ilk 39 | *.meta 40 | *.obj 41 | *.pch 42 | *.pdb 43 | *.pgc 44 | *.pgd 45 | *.rsp 46 | *.sbr 47 | *.tlb 48 | *.tli 49 | *.tlh 50 | *.tmp 51 | *.tmp_proj 52 | *.log 53 | *.vspscc 54 | *.vssscc 55 | .builds 56 | *.pidb 57 | *.svclog 58 | *.scc 59 | *.dat 60 | 61 | # Chutzpah Test files 62 | _Chutzpah* 63 | 64 | # Visual C++ cache files 65 | ipch/ 66 | *.aps 67 | *.ncb 68 | *.opensdf 69 | *.sdf 70 | *.cachefile 71 | 72 | # Visual Studio profiler 73 | *.psess 74 | *.vsp 75 | *.vspx 76 | 77 | # TFS 2012 Local Workspace 78 | $tf/ 79 | 80 | # Guidance Automation Toolkit 81 | *.gpState 82 | 83 | # ReSharper is a .NET coding add-in 84 | _ReSharper*/ 85 | *.[Rr]e[Ss]harper 86 | *.DotSettings.user 87 | 88 | # JustCode is a .NET coding addin-in 89 | .JustCode 90 | 91 | # TeamCity is a build add-in 92 | _TeamCity* 93 | 94 | # DotCover is a Code Coverage Tool 95 | *.dotCover 96 | 97 | # NCrunch 98 | _NCrunch_* 99 | .*crunch*.local.xml 100 | 101 | # MightyMoose 102 | *.mm.* 103 | AutoTest.Net/ 104 | 105 | # Web workbench (sass) 106 | .sass-cache/ 107 | 108 | # Installshield output folder 109 | [Ee]xpress/ 110 | 111 | # DocProject is a documentation generator add-in 112 | DocProject/buildhelp/ 113 | DocProject/Help/*.HxT 114 | DocProject/Help/*.HxC 115 | DocProject/Help/*.hhc 116 | DocProject/Help/*.hhk 117 | DocProject/Help/*.hhp 118 | DocProject/Help/Html2 119 | DocProject/Help/html 120 | 121 | # Click-Once directory 122 | publish/ 123 | 124 | # Publish Web Output 125 | *.[Pp]ublish.xml 126 | *.azurePubxml 127 | ## TODO: Comment the next line if you want to checkin your 128 | ## web deploy settings but do note that will include unencrypted 129 | ## passwords 130 | #*.pubxml 131 | 132 | # NuGet Packages Directory 133 | packages/* 134 | ## TODO: If the tool you use requires repositories.config 135 | ## uncomment the next line 136 | #!packages/repositories.config 137 | 138 | # Enable "build/" folder in the NuGet Packages folder since 139 | # NuGet packages use it for MSBuild targets. 140 | # This line needs to be after the ignore of the build folder 141 | # (and the packages folder if the line above has been uncommented) 142 | !packages/build/ 143 | 144 | # Windows Azure Build Output 145 | csx/ 146 | *.build.csdef 147 | 148 | # Windows Store app package directory 149 | AppPackages/ 150 | 151 | # Others 152 | sql/ 153 | *.Cache 154 | ClientBin/ 155 | [Ss]tyle[Cc]op.* 156 | ~$* 157 | *~ 158 | *.dbmdl 159 | *.dbproj.schemaview 160 | *.pfx 161 | *.publishsettings 162 | node_modules/ 163 | 164 | # RIA/Silverlight projects 165 | Generated_Code/ 166 | 167 | # Backup & report files from converting an old project file 168 | # to a newer Visual Studio version. Backup files are not needed, 169 | # because we have git ;-) 170 | _UpgradeReport_Files/ 171 | Backup*/ 172 | UpgradeLog*.XML 173 | UpgradeLog*.htm 174 | 175 | # SQL Server files 176 | *.mdf 177 | *.ldf 178 | 179 | # Business Intelligence projects 180 | *.rdl.data 181 | *.bim.layout 182 | *.bim_*.settings 183 | 184 | # Microsoft Fakes 185 | FakesAssemblies/ 186 | 187 | # LightSwitch generated files 188 | GeneratedArtifacts/ 189 | _Pvt_Extensions/ 190 | ModelManifest.xml 191 | /RecursiveDelete.csproj.GhostDoc.xml 192 | /RecursiveDelete.sln.GhostDoc.xml 193 | -------------------------------------------------------------------------------- /Program.cs: -------------------------------------------------------------------------------- 1 | // Copyright © 2015, EPSITEC SA, CH-1400 Yverdon-les-Bains, Switzerland 2 | // Author: Pierre ARNAUD, Maintainer: Pierre ARNAUD 3 | 4 | using System.Collections.Generic; 5 | using System.Linq; 6 | using System.Threading.Tasks; 7 | 8 | namespace RecursiveDelete 9 | { 10 | public class Program 11 | { 12 | static void Main(string[] args) 13 | { 14 | if (args.Length != 1) 15 | { 16 | var programName = System.IO.Path.GetFileName (System.Environment.GetCommandLineArgs ()[0]); 17 | 18 | System.Console.Error.WriteLine ("Specify the path of a directory and it will be deleted recursively."); 19 | System.Console.Error.WriteLine ("You can also drop a folder on the program to delete it."); 20 | System.Console.Error.WriteLine (); 21 | System.Console.Error.WriteLine ("Usage:"); 22 | System.Console.Error.WriteLine (" {0} \"path to folder\"", programName); 23 | System.Console.Error.WriteLine (); 24 | System.Console.Error.WriteLine ("No confirmation will be asked. The folder, all its files and subfolders"); 25 | System.Console.Error.WriteLine ("will be deleted, not moved to the trash. The operation is irreversible."); 26 | 27 | System.Console.ReadLine (); 28 | System.Environment.Exit (1); 29 | } 30 | 31 | var path = System.IO.Path.GetFullPath (args[0]); 32 | 33 | Program.Delete (path, path); 34 | 35 | System.Console.WriteLine (); 36 | System.Console.WriteLine ("Longest path found had {0} characters", Program.maxPath.Length); 37 | System.Console.WriteLine (Program.maxPath); 38 | } 39 | 40 | private static void Delete(string path, string displayPath) 41 | { 42 | var fullDisplayPath = displayPath; 43 | 44 | if (fullDisplayPath.Length > Program.maxPath.Length) 45 | { 46 | Program.maxPath = fullDisplayPath; 47 | } 48 | 49 | if (displayPath.Length > 64) 50 | { 51 | displayPath = "..." + displayPath.Substring (displayPath.Length-61, 61); 52 | } 53 | 54 | if (Program.ExecuteOperation (() => System.IO.File.Exists (path))) 55 | { 56 | System.Console.WriteLine ("Delete file {0}", displayPath); 57 | 58 | Program.DeleteFile (path); 59 | 60 | return; 61 | } 62 | 63 | if (Program.ExecuteOperation (() => System.IO.Directory.Exists (path))) 64 | { 65 | var shortPath = Program.ShortenDir (path); 66 | 67 | System.Console.WriteLine ("Analyzing {0}", displayPath); 68 | 69 | var entries = Program.ExecuteOperation (() => System.IO.Directory.GetFileSystemEntries (shortPath)); 70 | 71 | if (entries != null) 72 | { 73 | foreach (var entry in entries) 74 | { 75 | Program.Delete (entry, System.IO.Path.Combine (fullDisplayPath, System.IO.Path.GetFileName (entry))); 76 | } 77 | } 78 | 79 | System.Console.WriteLine ("Delete dir {0}", displayPath); 80 | 81 | Program.DeleteDir (shortPath); 82 | } 83 | } 84 | 85 | private static void DeleteFile(string path) 86 | { 87 | Program.ExecuteOperation (() => System.IO.File.Delete (path)); 88 | } 89 | 90 | private static void DeleteDir(string path) 91 | { 92 | Program.ExecuteOperation (() => System.IO.Directory.Delete (path)); 93 | } 94 | 95 | private static T ExecuteOperation(System.Func operation) 96 | { 97 | T result = default (T); 98 | 99 | Program.ExecuteOperation (() => 100 | { 101 | result = operation (); 102 | }); 103 | 104 | return result; 105 | } 106 | 107 | private static void ExecuteOperation(System.Action operation) 108 | { 109 | try 110 | { 111 | operation (); 112 | } 113 | catch (System.IO.IOException ex) 114 | { 115 | Program.Display (ex); 116 | } 117 | catch (System.UnauthorizedAccessException ex) 118 | { 119 | Program.Display (ex); 120 | } 121 | } 122 | 123 | private static void Display(System.Exception ex) 124 | { 125 | var color = System.Console.ForegroundColor; 126 | 127 | System.Console.ForegroundColor = System.ConsoleColor.Red; 128 | System.Console.Error.WriteLine (ex.Message); 129 | System.Console.ForegroundColor = color; 130 | } 131 | 132 | private static string ShortenDir(string path) 133 | { 134 | var root = System.IO.Path.GetDirectoryName (path); 135 | var name = System.IO.Path.GetFileName (path); 136 | 137 | var oldPath = System.IO.Path.Combine (root, name); 138 | var newPath = System.IO.Path.Combine (root, "@"); 139 | 140 | if (oldPath != newPath) 141 | { 142 | try 143 | { 144 | System.IO.Directory.Move (oldPath, newPath); 145 | } 146 | catch (System.IO.IOException ex) 147 | { 148 | System.Console.WriteLine (ex.Message); 149 | return oldPath; 150 | } 151 | } 152 | 153 | return newPath; 154 | } 155 | 156 | static string maxPath = ""; 157 | } 158 | } 159 | --------------------------------------------------------------------------------