windows - How do I delete a folder which is nested quite deep and avoid "File name too long"?

24
2014-04
  • user39186

    Eclipse created a temp folder in one of the directories which is nested quite deep, e.g.

    dir1\dir1\dir1\dir1\...
    

    I am unable to delete this folder in Windows via Explorer, the del or rmdir commands, nor the Cygwin 'rm' command. How should I remove this very long folder?

    It just keeps saying "File name too long..."

  • Answers
  • Synetech

    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:

    1. Rename C:\Dir1 to C:\D
    2. Navigate to C:\D\
    3. Rename C:\D\Dir1 to C:\D\D
    4. Navigate to C:\D\D\
    5. 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 Folderfor 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.

  • Synetech

    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:

    1. cd c:\subdir\more to cd into its parent directory.
    2. mkdir empty to create an empty directory.
    3. robocopy empty offending_dir /mir to mirror the empty directory into the offending one.
    4. After some waiting you're done! Finish it up with:
    5. rmdir offending_dir to get rid of the now empty offending directory and
    6. rmdir empty to get rid of your intermediate empty directory.
  • Matthew Simoneau

    You can shorten the path by using subst to create a virtual drive:

    C:\>subst Z: "C:\TEMP\dir1\dir1\dir1\dir1\dir1\dir1\dir1\dir1\dir1\dir1\dir1\dir1\dir1"
    

    Change into the virtual drive:

    cd Z:
    

    Now you can delete the files:

    del *.*
    

    Remove the virtual drive:

    cd C:\TEMP
    subst Z: /d
    

    Remove the directory:

    rd /s dir1
    
  • 8088

    I wrote a small C# app to help me delete a similar very deep structure generated by a careless usage of Robocopy and a backup from Homeserver; by default Robocopy treats joint points as regular folders... :-( You might end up with a big mess without noticing it.

    The tool is available at CodePlex with source files, for anyone to use.

    http://deepremove.codeplex.com

  • slhck

    While working with Sikuli I got jacked with a Calculator.sikuli recursion loop in the program that made an uncountable amount of "calculator.sikuli.calculator.sikuli" dirs. I could move the tree, but pathname too long to delete.

    After trying several solutions with popd loop, Scandisk and getting (perceptibly) nowhere....

    I wrote this script to 'go deep' into the recursed dirs(in a dir called 'a'), move them(to a dir called 'b'), then delete the truncated tree, move them back(to 'a'), and repeat:

    1)cd D:\a\calculator.sikuli\calculator.sikuli\calculator.sikuli\calculator.sikuli
    .............go deeeeeep in         dir *A*
    2) move calculator.sikuli ---> D:\b    
    .............move the crazy tree to dir *B*    
    3) kill D:\a\calculator.sikuli <---KILL(rd)    
    .............wipe dir *A*'s tree    
    4) move D:\b\calculator.sikuli ---> D:\a\    
    .............move the crazy tree back to dir *A*    
    REPEAT
    
    • REM Used to delete infinitely recursed subfolders
    • REM suggest to stop Windows Search service first(services.msc)

    Remdirs.bat

    D:
    cd D:\a\calculator.sikuli\calculator.sikuli\calculator.sikuli\calculator.sikuli
    move /-Y calculator.sikuli D:\b
    cd D:\b
    rd /s/q D:\a\calculator.sikuli
    move /-Y calculator.sikuli D:\a
    call D:\remdirs2.bat
    

    This is just a call to run the batch file again.

  • Jesse S.

    I would try opening a command prompt and running:

    rmdir /s <directory>
    

    If that doesn't work, I'd cd partway into the directory tree and try to delete a subset of the directories -- say the 20 innermost directories -- and then work my way out from there.

  • Punnakadu

    If it is a network folder then just share that directory's parent directory and map it to a drive on your local machine and then delete your folder.

  • XQYZ

    We had a problem like this at work when eclipse decided to create rubbish on the harddrives. We fixed it by using robocopy's /MIR function to mirror an empty directory into the nested one.

  • Loren Pechtel

    Another solution: go download Total Commander. It's a very useful program, not just because it's long filename aware.

    The unregistered version is nagware but fully functional, it will do the job.

  • techie007

    Open a command prompt.

    Navigate to the folder/directory that contains the highest 'dir1' (we'll assume C:\)

    c:\> RD /s dir1
    

    Edit (after comments added):

    Other ideas:

    MS offers info on how to deal with the problem (lots of ideas to try) here.

    There's also this tool (never used it personally) - TooLongPath.

    Perhaps write something (since you have Eclipse) that navigates all the way in and then backs out one folder level at a time, deleting as it goes?

  • music2myear

    When I have this problem I simply rename some of the folder names much shorter, then once the total path is short enough, it'll delete OK. No extra tools needed.

  • mrshl

    I had the same problem, except it was created by a recursive Cobian Backup task. I turns out the free Cobian software includes a Deleter application that can easily remove these pesky nested folders super quickly.

    It's located under the tools menu.

  • cantfork

    Your filesystem may be corrupt. Run chkdsk to see if it repairs anything, then try deleting the folder.


  • Related Question

    How to delete a file in Windows with a too long filename?
  • user3048

    Possible Duplicate:
    Tool for deleting directories with path/names too long for normal delete

    My wife has several files and folders that somehow ended up having filenames that have caused them to be undeleteable (can't be deleted) by normal means or via the command line. I believe the filenames are too long due to the depth of the folder structures. Does anyone know of a good utility for cleaning up files like this?


  • Related Answers
  • Will Eddins

    From a command prompt:

    dir /X
    

    This will list your files/folders in short name format. Then use the short name exactly as written to delete the file:

    del LONGF~1.txt
    
  • Benoit

    When you want to completely delete a directory and it has file with long names inside it, robocopy does a VERY good job:

    mkdir empty_dir
    robocopy empty_dir the_dir_to_delete /s /mir
    rmdir empty_dir
    rmdir the_dir_to_delete
    
  • joeqwerty

    I progressivley work my way into the path, renaming each successive parent folder to "1" and attempting to delete. You're effectively shortening the path each time and I've never had to work in by more than 4 or 5 directories until I'm finally able to delete the entire directory structure (which may or may not be what you want). You could do this from the last child folder as well and work your way up or down.

  • grawity

    In some programs, including Command Prompt (cmd.exe), you can get around the file length limit by prefixing the full path with \\.\ like this:

    \\.\C:\some directory\other directory\a file with long name
  • ldigas

    Rename the directory of cut/paste the file somewhere else, then delete it. Works here.

    Or just from the command prompt, if you don't feel like going through the trouble.

  • AdamV

    A trick I have used to get round the "full path and filename" length limitation in order to move, copy or delete something is to shorten it by 'breaking in' halfway down (or more) using a mapped drive letter pointing to a folder way down the path.

    so you have c:\some\long\path...\and\foo\bar\folders\oldfiles\myoldfile.txt.

    Then map an arbitrary drive letter to somewhere along the path so that the first chunk of the path becomes only a few characters long. Pre-requisite - the folder must be in a shared folder (which it may already be if it is on a server, which is where I have needed to do this), and if it is not already then pick a folder somewhere in the path and share it. Depending on your environment and paranoia level, allow everyone modify access to the share as long as the NTFS permissions are reasonably restrictive. If you want, just allow modify rights only to your own account.

    Now go to the shared folder or one inside it and share it, or use the command line as follows. Assume you shared folder "foo" as "fooshare", then you could do

    net use x: \\mycomputername\fooshare\bar\folders /persistent:no
    

    and the X: drive now points directly to the folder "folders" inside that share, so "x:\oldfiles\myoldfile.txt" is now pretty short.

    (The "/persistent:no" means this won't survive the next reboot and confuse you later on. Don't forget to un-share your folder when done.)

    Remember, you don't have to share the folder containing the file necessarily, if it is already inside a shared folder you can just map through the share and the nested folders to a target folder near to the file and that works fine.

    I've had to use this technique doing a massive robocopy between two servers when we realised that users had mapped drives quite deep in the folder structure, so they had been able to use 255 characters from there, but that exceeded the total file path length when accessed from the local drive root.

  • Col

    The simplest way I've found is to boot from an ubuntu live CD.

    As an alternative you can create a shared folder halfway down the path and then map a network drive to that and do the delete from the mapped folder (even on the same machine)

  • William Hilsum

    Probably not the best way and I am interested to see what others come up with -

    I had this once and I tried a few things without any luck. Rather than looking for a good tool, I restarted with the Windows Disk in, went to the recovery console and just deleted it from there. Worked first time and really well!

    Also, just did a Google for you and found this - DelinvFile Looks Good but cannot vouch for it.

    Edit - Warning, just seen the above is only a trial - Maybe not as good as I first thought!

  • Bob

    You could always try Unlocker as it has delete capabilities for 'awkward' files.