假设我有一个目录,其中包含以下文件:
Test.bat Test_a.txt Test_b.txt Test_v1.zip Test_v2.zip Test_v3.zip
我想Test_v*.zip
悄悄地删除所有内容(没有错误消息记录到屏幕上)。我可以使用以下脚本来实现这一点:
@ECHO OFF
SET OLD_ZIPS=^
C:\Tmp\Test_v*.txt;^
C:\Tmp\Test_a.txt
ECHO Deleting the following files: %OLD_ZIPS%
FOR %%Y IN (%OLD_ZIPS%) DO (
IF EXIST %%Y (
ECHO Deleting %%Y
DEL /Q %%Y)
)
PAUSE
这工作正常:
Deleting the following files: C:\Tmp\Test_v*.txt;C:\Tmp\Test_a.txt
Deleting "C:\Tmp\Test_v1.txt"
Deleting "C:\Tmp\Test_v2.txt"
Deleting "C:\Tmp\Test_a.txt"
Press any key to continue . . .
当然,除非文件路径包含空格。所以在上面的例子中,如果我C:\Tmp\Test_v*.txt
改为C:\Tmp with spaces\Test_v*.txt
我得到:
Deleting the following files: C:\Tmp test\Test_v*.txt;C:\Tmp test\Test_a.txt
Press any key to continue . . .
我怎样才能阻止它在空间上犹豫不决?
编辑- 我已经按照 Alex K 的回答尝试了空格(加上更多的调试),看起来 for 循环可能没有像我预期的那样把事情分开:
@ECHO OFF
SET OLD_ZIPS=^
C:\Tmp test\Test_v*.txt;^
C:\Tmp test\Test_a.txt
ECHO Deleting the following files: %OLD_ZIPS%
FOR %%Y IN (%OLD_ZIPS%) DO (
ECHO Checking existance of "%%Y"
IF EXIST "%%Y" (
ECHO Deleting "%%Y"
DEL /Q "%%Y")
)
PAUSE
..给我:
Deleting the following files: C:\Tmp test\Test_v*.txt;C:\Tmp test\Test_a.txt
Checking existance of "C:\Tmp"
Checking existance of "C:\Tmp"
Checking existance of "test\Test_a.txt"