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


Recent Tweets

@ACLU n to pay for a long time. I yu the
#Xamarin Certified Trophy arrived today! @XamarinU thanks!
Retweeted by @dyardy #PowerShell and Kerberos double hop. Slides, module, and demos. #PshSummit
@danrigby @Raspberry_Pi Very cool, let us know how it goes, thx