How to delete a file with a path too long to be deleted [duplicate]
I was working with some computer vision source code I found and didn't realize that it creates a cache file that has a VERY LONG name and now I can't delete the 2 of them.
I tried to rename it to a shorter name but I can't do anything to the file, I also tried to restart my computer and see if it would just go away.
The error message says:
Destination Path Too Long.
The file name(s) would be too long for the destination folder. You can shorten file name and try again, or try a location that has a shorter path.
How can I delete the files in this case?
Solution 1:
Assuming you are on windows:
What I always do is use subst to create a drive letter association with part of the path.
Then go to the new drive letter and navigate to the files that have long names. You should now be able to rename/delete/etc them. The reason this works is because the path itself is no longer containing >255 chars.
To create a subst folder
- Start a command prompt (no admin privileges needed)
- Use
cd
to navigate to the folder you want to go (you can use tab to autocomplete names - type
subst j: .
to create the driveletter association. (instead of the . you can also type the entire path) - Now in Explorer, you have a new drive letter in This PC. Go to it and do whatever you need to do to the .cache files.
- Return to your cmd window and type
subst /d j:
to remove the drive or alternatively, restart your pc.
EDIT: A trick I learned which can shorten this quite a bit. From the Explorer window where you have trouble, click the address bar so you can type in it, then type cmd
and press Enter to start a command prompt at that location, then proceed at step 3. :)
EDIT2: Okay, this trick can be expanded even further.
If you are in explorer at the right path, just type in subst j: .
in the address bar and press enter. A command window will pop up, execute the command and close again, and there you go, a new drive pops up in explorer.
Solution 2:
Your question asks how to delete a file when the filename itself is too long. If the path is too long and not just the filename itself, you can easily just rename the folders to make the path shorter.
But to delete a file whose name is more than 255 characters:
- Open a command prompt by running "CMD.EXE"
- Navigate to the folder holding the file
- Use the command DIR /X which will display the short names of files.
- Delete using the short name.
i.e. if the file is named "verylongfilename.fil", the shortname will display as something like "verylo~1.fil" and you can delete using that name.
Please note that although Windows itself can handle paths far longer than 255 characters, most of the GUI -- like Windows Explorer -- is bound by the shorter limit.
Solution 3:
Googled: delete long filename First result
Use Unlocker http://www.filehippo.com/download_unlocker/
Cannot delete folder: It is being used by another person or program Cannot delete file: Access is denied There has been a sharing violation. The source or destination file may be in use. The file is in use by another program or user. Make sure the disk is not full or write-protected and that the file is not currently in use.
Unlocker can help! Simply right-click the folder or file and select Unlocker. If the folder or file is locked, a window listing of lockers will appear. Simply click Unlock All and you are done!
Solution 4:
Microsoft has a useful knowledge base article on deleting undeletable files. Cause #4 is of relevance to you.
Your best bet would probably be to map a drive to the folder that file is in (resolution #3), then enter in through that drive and delete it.