Solved: "Cannot read from the source file or disk"

I’ve finally solved a problem that’s been bugging me for years. One of our file shares ended up with several undelete-able files. Attempting to delete them results in “Error Deleting File or Folder - Cannot delete file: Cannot read from the source file or disk“.

Even going to the file’s properties to check permissions presented a very blank properties dialog. And a CHKDSK didn’t sort thing out either.

It turns out the problem was: the filename ended with a dot, e.g. it was something like “C:\Temp\Stuff\Sales Agreement.“. As far as Windows is concerned this is an invalid file name: so although it gets reported in a directory listing, the standard Windows APIs for manipulating files subsequently deny its existence.

So how did this file get created in the first place? The answer: a Mac. The file was on a file share which had been accessed by a Mac user. Macs tend to write all sorts of metadata to extra “._DSStore” files and suchlike and had left this file behind.

So if Windows doesn’t appear to allow these file names, how did they get to be created? Well, it turns out that NTFS allows all sort of file name/path weirdness that Windows, or specifically the Win32 API, doesn’t allow. For example, NTFS actually allows file paths up to 32K but Windows restricts file paths to no more than 260 characters (MAX_PATH). I suppose this is all for DOS/Windows 9x backwards compatibility. As these files were being accessed over a file share I guess the usual Win32 checks are bypassed.

But thankfully you can get Win32 to ignore these checks by prefixing your file paths with \\?\, (ie. C:\Temp\SomeFile.txt becomes \\?\C:\Temp\SomeFile.txt) which I discovered after reading this blog post about long paths in .NET.

So at a command prompt I was able to delete the file using:

del "\\?\C:\Temp\Stuff\Sales Agreement."

Of course the corollary of this is that you could really annoy somebody by doing this:

echo Hi > "\\?\%USERPROFILE%\Desktop\Annoying file you can't delete."

But you wouldn’t do that would you?

17 Comments so far

  1. James on June 12, 2008

    At the end of reading this, I actually glanced over to the side of my desktop to see if there was a file there called ‘Annoying file you can’t delete.’ It was entirely involuntary. Clearly on a subconscious level I have deep trust issues.

  2. -=N=- on June 12, 2008

    INTRIGUING.

    Thanks for the heads up on that Duncan.

  3. Tatyana on June 23, 2008

    I’ve inherited a bunch of files ending in “.” and had no idea how to get rid of them, and now this simple and elegant solution. Thank you very much, Duncan!

  4. Tech Guy on June 27, 2008

    Sweeeeeet! Worked like a charm. I had tried booting in safe mode and deleting, MoveOnBoot, and toying with permissions, none of which worked. The files did indeed come from a Mac and in my case it wasn’t a period on the end, but something that looked like a space. I highlighted the name of the offending file, copied it, got in the command prompt window, typed your command, (del “\\?\C:\folder name\sub folder name\), then right-clicked to get the paste function in the command prompt window and typed the close quote. That way, no matter what the offending character is, there’s no guess work. Thank you!

  5. Nation on June 29, 2008

    Very helpful! Glad I found this via Google. My problem was a folder name generated by a program. I tried to move it, rename it, etc — no dice. In my case the last character in the name appeared to be a space. I couldn’t seem to rename it with this trick (got syntax error - sorry, I didn’t record exact error message). So I copied the contents into another folder, and then deleted the bad folder ( rd “\\?\c:\folder\folder\bad name”). It went away. Some where in some manual the exact syntax rules of these DOS commands is documented, I’m sure.

  6. Nation on June 29, 2008

    Oh, and I couldn’t resist trying your evil suggestion. Yes, it worked on my (Win XP NTFS) system. And the same method deleted it. There may be some hacker potential for this method of creating (practically) undeletable files.

  7. Aris on July 2, 2008

    Great! It worked like magic. Thanks. :-)

  8. borg on July 13, 2008

    ok so im not very computer savy and i still cannot get a file on my desktop to delete it gives me this same error

  9. Lindsay George on July 18, 2008

    In addition to this you can try reading this:

    http://www.softwaretipsandtricks.com/forum/windows-xp/32482-error-message-cannot-delete-file-cannot-read-source-file-disk.html

    This is unlocker(it’ll do the job without having you type in names check it out!): http://ccollomb.free.fr/unlocker/

  10. DRev on July 25, 2008

    Thank you. Very much.

  11. Jimmy Jim on July 29, 2008

    I just wanted to thank you for posting this. It helped me get rid of an annoying file on my desktop :)

  12. Korvus Redmane on August 4, 2008

    Thanks. I had a file which i think was some kinda deleated download which i couldn’t remove, but this worked.

  13. Edward on August 7, 2008

    So I have little experience with command prompt. How exactly do I go about prefixing the file path to the file I want to delete?

  14. Jason on August 14, 2008

    doesn’t work with Win XP SP3

  15. augerin on August 25, 2008

    great! thanks.

  16. LC on August 30, 2008

    @Lindsay George the unlocker program worked for me, thanks.

  17. Chrome on September 1, 2008

    Good lord, thanks for this. I tried ALL the other things, and this is the only method that did it. In my case, it was a space at the end of too-long filenames.(It worked even though I have SP3!)

    Perhaps these WERE a deliberate trap…

Leave a reply