Well, it’s fairly simple to delete files and folder with destination path too long and name. In windows world, you will act a little over smart and witty, map the path as mapped drive and delete the file/folder. But what if, you have a billion files (may be that’s little bit too much 🙂 ) that you need to remove, you can’t keep mapping the drive and deleting the files.
There is better way of handling this situation. The solution doesn’t come from Windows world however it comes from Linux (quite amazing & amusing) world which works pretty well on windows.
So here is how it goes.
You need to download and install Cygwin (A linux emulator on windows). You can download Cygwin from here https://cygwin.com/install.html
It comes in both x86 (32 bit) and x64 versions. The setup is under 1 mb and uses internet connection to download dependencies and packages to install full functional Cygwin emulator.
Note: In case you are looking for portable version of Cygwin, you can download it from www.cybesystems.com
nus.edu.sg is the nearest location to where I am based and It’s National University of Singapore. You may want to select some other location based on your region.
By default, the dependencies packages would be selected automatically and you don’t need to select any other package.
Once Installed, you can launch the Cygwin terminal from Start > or directly from the install location.
In above real world issue, I was working on the DFS-R replication issue, where I had to purge DFSR folder under System Volume Information and delete the contents. Now, literally, it has hundreds of thousands of file which could be deleted otherwise, due to path Destination Path Too Long issue.
Notice, the way you browse to the drive and directory is different from windows.
Once you are under at the correction location, issue the following command
you can get the view the help using the following command
rm --help Usage: rm [OPTION]... [FILE]... Remove (unlink) the FILE(s). -f, --force ignore nonexistent files and arguments, never prompt -i prompt before every removal -I prompt once before removing more than three files, or when removing recursively; less intrusive than -i, while still giving protection against most mistakes --interactive[=WHEN] prompt according to WHEN: never, once (-I), or always (-i); without WHEN, prompt always --one-file-system when removing a hierarchy recursively, skip any directory that is on a file system different from that of the corresponding command line argument --no-preserve-root do not treat '/' specially --preserve-root do not remove '/' (default) -r, -R, --recursive remove directories and their contents recursively -d, --dir remove empty directories -v, --verbose explain what is being done --help display this help and exit --version output version information and exit By default, rm does not remove directories. Use the --recursive (-r or -R) option to remove each listed directory, too, along with all of its contents. To remove a file whose name starts with a '-', for example '-foo', use one of these commands: rm -- -foo rm ./-foo Note that if you use rm to remove a file, it might be possible to recover some of its contents, given sufficient expertise and/or time. For greater assurance that the contents are truly unrecoverable, consider using shred. GNU coreutils online help: <http://www.gnu.org/software/coreutils/> Full documentation at: <http://www.gnu.org/software/coreutils/rm> or available locally via: info '(coreutils) rm invocation'
Now we would use the rm (remove) command with few switches to delete the directory containing files with long names and destination.
rm -f -r -d -v “foldername”
The above command would delete the parent directory and everything underneath without being stopped for destination path too long error.
Important: Note that the above screenshot doesn’t show DFSR.old path however the command rm was run under DFSR.old directory. Don’t run it under System Volume Information.
Hope this helps.