i am trying to delete some files using a batch file.. (winxp)
我试图使用批处理文件删除一些文件..(winxp)
my problem is when i delete using a wildcard.. it is matching the 8.3 name aswell as the long name.
我的问题是当我使用通配符删除..它匹配8.3名称以及长名称。
eg: file list
例如:文件列表
file1.pyfile1.pycfile2.pycstlongnamefile2.pycstlongnamecif i do a
如果我做了
Del *.pycit deletes everything but file1.py
它会删除除file1.py之外的所有内容
becuase if i do a dir /X all of teh 8.3 shortnames end with .PYC
因为我做了一个目录/ X所有的8.3短号以.PYC结尾
2 个解决方案
#1
When you perform file management using wildcards from the command prompt, files with long extensions can be unexpectedly displayed, copied, or deleted.
从命令提示符使用通配符执行文件管理时,可能会意外显示,复制或删除具有长扩展名的文件。
You are right, it happens on my computer too, running fully-patched Windows XP SP3.
你是对的,它也发生在我的电脑上,运行完全修补的Windows XP SP3。
See the following Microsoft Knowledgebase Article for more information, and a registry change that will solve it. This one goes all the way back to Windows NT 4.0:
有关详细信息,请参阅以下Microsoft知识库文章,以及将解决此问题的注册表更改。这一个一直回到Windows NT 4.0:
Article ID: 164351 - Last Review: November 1, 2006 - Revision: 1.1 Command Prompt's Treatment of Long File Extensions
文章编号:164351 - 最后修改:2006年11月1日 - 修订:1.1命令提示符处理长文件扩展名
http://support.microsoft.com/kb/164351
UPDATE: This behavior also occurs in a test C# application I wrote. The behavior is disturbing enough that I applied the registry fix to my computer, and it does work.
更新:这种行为也发生在我写的测试C#应用程序中。这种行为令人不安,我将注册表修复程序应用于我的计算机,它确实有效。
#2
Well, in your case you can probably use forfiles:
那么,在你的情况下你可以使用forfiles:
> forfiles /m *.pyc"file1.pyc"it does not adhere to cmd's wildcard expansion rules.
它不符合cmd的通配符扩展规则。
You can also enumerate all files and filter for extension afterwards:
您也可以枚举所有文件并在之后过滤扩展名:
> for %i in (*) do @if %~xi==.pyc @echo %ifile1.pycBoth methods skip the files where only the .3 part of the extension matches.
两种方法都跳过只有扩展名的.3部分匹配的文件。
So you can use either
所以你可以使用其中之一
forfiles /m *.pyc /c del @FILEor
for %i in (*) do @if %~xi==.pyc @del "%i"Admittedly, a little more complex than a simple del *.pyc, though.
不可否认,比简单的del * .pyc稍微复杂一点。
ETA: Since someone was sceptical whether this would work, a little snippet from a cmd session on a Win XP SP 2 VM:
ETA:由于有人怀疑这是否有效,因此在Win XP SP 2 VM上使用cmd会话获得一小段代码:
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 /bfile1.pyfile1.pycfile2.pycstlongnamefile2.pycstlongnamecS:\Temp>del *.pycS:\Temp>dir /bfile1.pyS:\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 /bfile1.pyfile2.pycstlongnamefile2.pycstlongnamecNevermind 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:
第一部分是微不足道的。对于(*)中的%i,只需枚举当前目录中的所有文件。然后我们检查扩展名; %~xi扩展为完整扩展(不是8.3内容),因此在行上方运行将导致运行以下命令:
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.
我希望很明显为什么这比包含8.3名称的cmd所做的全局更好。这里没有通配符,没有错。
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.
forfiles变体可以在我的Win 7和Vista机器上运行。我只能猜测,但我强烈怀疑,forfiles没有实现与cmd相同的通配符匹配规则。由于我们在Windows上,每个程序都会为自己扩展通配符。与UNIX系统不同,shell不会这样做。程序究竟是如何做到这一点有时不同,显然它也在这里。
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.
毋庸置疑,我认为forfiles的方法在这里更为明智,因为8.3名称很快就会死于可怕的死亡。或者至少我希望他们这样做。