I use Windows 7, and I a want to build a batch-file with to complete the following task:
Delete all files with 4 characters in name in the C:\images directory.
E.g. 1234.jpg 7123.jpg, 8923.jpg, 7812.jpg, 1245.jpg, 0067.jpg, 0001.jpg, 0010.jpg, 0060.jpg etc.
Is that possible?
This solution will erase all files whose name part before the dot have 4 characters, and possibly those that have a shorter part before the dot.
?stands for any character (or sometimes no character at all, see below)*stands for any sequence of characters.What different Windows versions ?
Under Windows 98 (and I have a gut feeling that this is also valid for “pure” DOS versions without full support for long FAT names, which would include win95 and older, and maybe Windows Millennium), the
?stands for exactly one character.Based on @dbenham’s comment, I get that in later versions of Windows,
?stands for 0 or 1 characters.What about FAT short names ?
Now for the tricky parts : What about names with a leading dot (i.e. 0 characters before the first dot) ? What about files with multiple dots ?
Trailing dots are stripped before anything is performed, so the extension of
abc.def.....isdef, and its base isabc.The dot is at a fixed position in the short name, and is present even for files without an extension. That’s why
*.*matches all files, even those without an extension.Leading dots, multiple dots, extensions of more than 3 characters and base (before the dot) parts of more than 8 characters are invalid in short FAT names (also called DOS names), so a file whose long name (displayed under Windows) is one of those will have a made-up short name, looking like
POBASE~N.POE, where :POBASEis a part of the base (usually the beginning, stripped of unusual characters),~is a literal tilde character,Nis a single or multiple-digit number, and is usually the smallest number that doesn’t clash with existing names,POEis a part of the extension (usually the beginning, stripped of unusual characters), where the extension is the part after the last dot,I created the following files, in that order :
.abhad the short nameAB~1a.b.chad the short nameAB~1.Ca.bcdehad the short nameA~1.BCDa.x y(notice the space) had the short namea.xy..abhad the short nameAB~2, sinceAB~1was already there (first example)a.b,a.b.,a.b..,a.b...are all equivalent names for the same file (i.e. to createa.b...will overwritea.bif it exists).When you run the command
dir ????.*on an old Windows 98 system, the following files will be matched :AB~1, with long name.abAB~1.C, with long namea.b.cAB~2, with long name..abNow, while Vista and 7 have tried to throw most DOS legacy out of the window (pun intended), there are still some remnants : if you type
C:\PROGRA~1in the address bar of an explorer window, you’ll land into (the half-baked localized variant of)C:\Program files, and if you plug in a FAT-formatted USB key, the files will have short names. I’m not sure if the wildcards will match against these short names, but here are some “fun” facts, that show it’s not even worth investigating :Fun facts
C:\Program fileswill show up in explorer asC:\Programmes, but if you try tocd C:\Programmes, it will fail, so the translated names don’t seem to be used by the command-line.????????.?(8.1 question marks) will match witha.b.c(which is only 5 characters), but anything shorter (like???????.?with 7.1 question marks) won’t match it.????????.?will matchfoo, although there is no dot infoo.Conclusion
To conclude, if you want to write a program that reliably deletes files with exactly four characters before the first (or last) dot, use some of the other solutions, don’t use wildcards.
However, be warned that older windows versions don’t have all those fancy-dancy scripting capabilities for getting the length of a string with awkward syntax (I mean, seriously,
if "!n:~3,1!" neq "" echo del "%%F", it’s nearly as bad as zsh globbing modifiers.All this would be so much simpler if windows didn’t have three layers of file naming (FAT short names, “actual” file name, and translated file name), and if they hadn’t chosen to change the meaning of
?.Sigh