Demartek Comments: Deleting Files with Long File Names
14 July 2015
By Dennis Martin, Demartek President
A friend recently mentioned that he was having trouble deleting a file with a long file name on a Microsoft Windows system. He tried several options, including PowerShell, Windows Explorer and others, but was not able to delete the file.
I replied that I have seen that problem here at Demartek. The solution is a bit tedious, but does work. The problem is that although the file name itself may be within the allowed length, the delete function internally uses the full path name, including all the folder/directory names, when it executes. In some cases, the full path name may be too long, so the delete command fails with an error.
To delete such a file that is typically deep down in a directory structure, I follow this process. Starting at the top-level folder, I rename the folder to a shorter name (one or two characters, for example) and then attempt to delete the file. If this does not work, I then move one level lower in the directory tree, rename that folder to a shorter name and attempt to delete the file. I continue this pattern until I am able to delete the file. I generally perform this function from a command line.
If you are trying to delete all the files in an entire directory tree, this method can work well if there are multiple folders at some lower point in the directory tree with long filenames. You might get to a common point in the tree to permit files from several folders to be deleted at the same time.
Another option in a Command Prompt or PowerShell window would be to navigate to the folder with the long filename (assuming that you have found it) and use the “subst” command to associate a new drive letter association to that place in the path. Then you can navigate to the new drive letter and operate from there.
There are various reasons that such a file could be created that cannot be immediately deleted due to the full length of the filename including the full path information. An application may be creating files in the current directory, files could have been moved into existing folders, files could have been created by another system, etc.
Demartek public projects and materials are announced on a variety of social media outlets. These include Facebook, LinkedIn, Twitter and YouTube. Click any of the icons below to visit our pages.