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

I came across a situation where I had to remove all files at this location C:\Users\{username}\AppData\Local\Temp.  I was able to delete most of the files however I came across a Windows error stating that one or more files had a file name too long to be removed.  I tried a number of approaches (not to be stated here) however the resolution was to use RoboCopy.

I created a local directory c:\temp\empty_dir with nothing in it.

I then used the following RoboCopy command to mirror that empty directory with the problematic directory.

robocopy c:\temp\empty_dir C:\Users\{username}\AppData\Local\Temp /s /mir

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




comments powered by Disqus