Well, in your case you can probably use forfiles
:
> forfiles /m *.pyc
"file1.pyc"
it does not adhere to cmd's wildcard expansion rules.
You can also enumerate all files and filter for extension afterwards:
> for %i in (*) do @if %~xi==.pyc @echo %i
file1.pyc
Both methods skip the files where only the .3 part of the extension matches.
So you can use either
forfiles /m *.pyc /c del @FILE
or
for %i in (*) do @if %~xi==.pyc @del "%i"
Admittedly, a little more complex than a simple del *.pyc
, though.
ETA: Since someone was sceptical whether this would work, a little snippet from a cmd session on a Win XP SP 2 VM:
S:\Temp>for %i in (file1.py,file1.pyc,file2.pycstlongname,file2.pycstlongnamec) do @copy nul %i
1 Datei(en) kopiert.
1 Datei(en) kopiert.
1 Datei(en) kopiert.
1 Datei(en) kopiert.
S:\Temp>dir /b
file1.py
file1.pyc
file2.pycstlongname
file2.pycstlongnamec
S:\Temp>del *.pyc
S:\Temp>dir /b
file1.py
S:\Temp>del *
S:\Temp>for %i in (file1.py,file1.pyc,file2.pycstlongname,file2.pycstlongnamec) do @copy nul %i
1 Datei(en) kopiert.
1 Datei(en) kopiert.
1 Datei(en) kopiert.
1 Datei(en) kopiert.
S:\Temp>for %i in (*) do @if %~xi==.pyc @del "%i"
S:\Temp>dir /b
file1.py
file2.pycstlongname
file2.pycstlongnamec
Nevermind the German messages, I hope it shows that above method does in fact work.
To explain a bit why it works, we can take a look at the command:
for %i in (*) do @if %~xi==.pyc @del "%i"
The first part is trivial. for %i in (*)
simply enumerates all files in the current directory. We then check for the extension; %~xi
expands to the full extension (not 8.3 stuff), so running above line will cause the following commands to be run:
if .py == .pyc del "file1.py"
if .pyc == .pyc del "file1.pyc"
if .pycstlongname == .pyc del "file2.pycstlongname"
if .pycstlongnamec == .pyc del "file2.pycstlongnamec"
I hope it is obvious why this works better than the globbing done by cmd which includes 8.3 names. No wildcards here, nothing to do wrong.
The forfiles
variant does work on my Win 7 and Vista machines here. I can only guess but I strongly suspect, forfiles does not implement the same wildcard matching rules as cmd. Since we are on Windows, every program expands wildcards for itself. This is not done by the shell, unlike on UNIX systems. How exactly programs do this differs sometimes and apparently it does here, too.
Needless to say that I consider forfiles
's approach here a little more sensible as 8.3 names should die a horrible death sometime soon. Or at least I hope they do.