cmd.exe: "del *.txt" also removes *.txt1 etc

It's an occasionally-encountered somewhat-known?

An explanation of the behavior is here, complete with a possible work-around.

Summary: It comes from the way NTFS handles short (8.3) filenames, for compatibility with older stuff that can't handle "new-fangled" long filenames. dir /x can be used to view the short filename, like so:

C:\>dir /x *~*
 ...
02/25/2011  08:18 AM    <DIR>          DOCUME~1     Documents and Settings
05/28/2011  12:02 AM    <DIR>          PROGRA~1     Program Files
 ...

Extensions longer than three characters will simply be truncated for their short filename; so .txt1, .txtxtxtxt, .txt_file, etc, all become .txt.


Files with more than 4 letters in the extension (and other rules of course also apply), get a short name, so for example "foo.txt1" has the shortname "foo~1.txt". DEL (like DIR and possibly most other cmd.exe builtin commands) will also operate on shortnames. For a discussion on a related topic see here: http://blogs.msdn.com/b/oldnewthing/archive/2005/07/20/440918.aspx


Would this be on a FAT/VFAT filesystem? If so, you're getting bitten by the hackery used to represent names that don't fit into the 8.3 schema; there's a DIR option that shows you the mangled short names, which will begin with the actual file name but have a ~ and a number before the extension, then the extension is capped at 3 characters. (Example: C:\Program Files usually translates to C:\PROGRA~1.) A wildcard may unexpectedly match these names. This should not happen on NTFS or (I think) on network shares. except that DOS still hasn't died, it seems....

Tags:

Command Line