How to delete files with weird characters in Windows

Updated: March 30, 2022

Every few years, I face this wee problem. I try to delete a file in Windows Explorer, and it throws an error, telling me it cannot find (and delete) the file specified. OK, happens. So I go to the command line, and do the deletion there. However, this time, I encountered a fresh snag. The usual deletion action wouldn't complete, with the command prompt saying: The system cannot find the file specified.

I encountered this issue with a lock file created by my Xerox B215 printer on a Samba share. For some odd reason, the printer truncated the actual filename, and instead of it being "Xerox_Scan.pdf", it was named just "Xerox_Scan." Ha, but was it really?


So, on the command line, we get this:

del "Xerox_Scan."
The system cannot find the file specified.

At this point, I realized that the file name probably has a weird trailing character that is not visible in the Windows Explorer or the command prompt. This in fact reminds me of a little trick that I and some colleagues would use in system administration exams for our juniors. We'd create a file with special characters at the end, like the old Alt + 255 trick in DOS, and then see who'd figure it out. In Linux, you can use the octal dump utility (od) to see the full set of characters, of course.


In Windows, the approach is to go with a greedy search (or deletion to be more precise). If you're wondering what's happening here, think regular expressions. We are looking for any file (/s) in any of the subdirectories in the specified path, and we're looking for a file that has zero or more trailing whitespace characters. Boom. Job done, no more ugliness.

del /s \\?\D:\Scans\Xerox_Scan."
Deleted file - \\?\D:\Scans\Xerox_Scan.


Technically speaking, an undeletable file shouldn't be a huge problem, but it is a problem on the OCD level, cosmetically, and, theoretically, it could interfere with backup scripts or scheduled tasks if they are not designed to gracefully ignore files of this kind. Therefore, you do want to do some light maintenance, after all.

There are many other methods you could use. For example, Process Explorer can also search for files and then delete them, including whitespace characters. But I find the simple command line regex-search execution to be the most elegant solution. Anyway, our task was annoying but it has a quick and painless solution. See you around.


You may also like: