How do I delete a folder which is nested quite deep and avoid "File name too long"?
If you are like me and don't like to install additional software to fix a problem like this, I'd go with XQYZ's suggestion and use robocopy
to solve the problem. (In my case the problem was created by robocopy in the first place, by copying a directory which had recursive junction points in it without supplying /XJ to robocopy).
To delete the directory tree starting at c:\subdir\more\offending_dir:
The total step-by-step-process is as simple as this:
-
cd c:\subdir\more
to cd into its parent directory. -
mkdir empty
to create an empty directory. -
robocopy empty offending_dir /mir
to mirror the empty directory into the offending one. - After some waiting you're done! Finish it up with:
-
rmdir offending_dir
to get rid of the now empty offending directory and -
rmdir empty
to get rid of your intermediate empty directory.
This is actually quite simple to fix. Say that the directory structure is as such:
C:\Dir1\Dir1\Dir1\Dir1…
To fix it, just rename each folder to a one-character folder-name until it is no longer too long to delete:
- Rename
C:\Dir1
toC:\D
- Navigate to
C:\D\
- Rename
C:\D\Dir1
toC:\D\D
- Navigate to
C:\D\D\
- Goto 1 until total length of path is <260
Here’s a batch file to automate the process (this simple version is best for simple directories like the one described in the question, especially for disposable ones). Pass it the highest folder possible (eg C:\Dir1
for C:\Dir1\Dir1\Dir1…
or C:\Users\Bob\Desktop\New Folder
for C:\Users\Bob\Desktop\New Folder\abcdefghi…
)
@echo off
if not (%1)==() cd %1
for /D %%i in (*) do if not %%i==_ ren "%%i" _
pushd _
%0
popd
Technical Explanation
The other proposed solutions are backwards; you can’t fix it by working your way from the innermost directory outward, you need to go in the other direction.
When you try to access a directory, you do so using its absolute path whether explicitly or not, which includes everything that came before it. Therefore, for a directory structure like C:\Dir1\Dir1\Dir1\Dir1
, the length of the path to the innermost Dir1
is 22. However the length of the path to the outermost Dir1
is only 7, and therefore is still accessible regardless of its contents (in the context of a given directory’s path, the file-system has no knowledge of what it contains or the effect it has on the total path length of its child directories; only its ancestor directories—you cannot rename a directory if the total path-length will be too long).
Therefore, when you encounter a path that is too long, what you need to do is to go to the highest level possible and rename it to a one-character name and repeat for each level therein. Each time you do so, the total length of the path shortens by the difference between the old name and new name.
The opposite is true as well. You cannot create a path that is greater than the maximum supported length (on DOS and Windows, MAX_PATH = 260
). However, you can rename directories, working from the innermost outward, to a longer name. The result is that deeper folders whose absolute path is >260 will be inaccessible. (That does not make them “hidden” or secure, since they are simple enough to get at, so don’t use this method to hide files.)
Interesting Side Note
If you create folders in Windows 7 Explorer, it may seem like Explorer allows you to create subdirectories such that the total length is longer than MAX_PATH
, and in effect it is, however it is actually cheating by using “DOS 8.3 filenames”. You can see this by creating a tree such as the following:
C:\abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
\abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
\abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
\abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
\abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
\abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
\abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
\abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
\abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
\abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789
\abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789\
It is 696 characters long, which of course is much longer than 260. Further, if you navigate to the innermost subdirectory in Explorer, it shows it as expected in the address bar when it is not in focus, but when you click in the address bar, it changes the path to C:\ABCDEF~1\ABCDEF~1\ABCDEF~1\ABCDEF~1\ABCDEF~1\ABCDEF~1\ABCDEF~1\ABCDEF~1\ABCDEF~1\ABCDEF~1\ABCDEF~1\
, which is only 102 characters long.
In XP, it does not do this, instead it steadfastly refuses to create a longer path than is supported.
What would really be interesting is to find out how Windows 7 Explorer handles “too-long paths” when the NtfsDisable8dot3NameCreation
option is set.