How to delete a file in Windows with a too long filename? [duplicate]
Solution 1:
When you want to completely delete a directory and it contains long paths, robocopy
does a VERY good job:
mkdir empty_dir
robocopy empty_dir the_dir_to_delete /mir
rmdir empty_dir
rmdir the_dir_to_delete
This works because robocopy internally uses the Unicode-aware versions of Win32 functions, with the \\?\
prefix for file paths; those functions have a limit of 2¹⁶-1 (32,767) characters instead of 259.
You may need to go through this process more than once to get rid of all of the files.
Solution 2:
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
Solution 3:
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.
Solution 4:
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.
Solution 5:
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