windows - 当路径包含空格时,如何使用通配符从 MS DOS 批处理文件中删除多个文件?

标签 windows batch-file filepath

假设我有一个目录,其中包含以下文件:

测试.bat 测试_a.txt 测试_b.txt 测试_v1.zip 测试_v2.zip 测试_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 space\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"

最佳答案

看来你试图让事情变得过于复杂。

for %a in ("C:\Tmp with spaces\Test_v*.txt" "C:\Tmp\Test_a.txt") do del /q "%a"

执行您想要的操作,并且可以从命令行键入。如果您想在批处理文件中执行此操作,请将 %a 更改为 %%a

关于windows - 当路径包含空格时,如何使用通配符从 MS DOS 批处理文件中删除多个文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12498877/

相关文章:

windows - 如果 Windows 中不存在目录,则使用 for 循环和列表创建目录

java - 如何使用命令访问 Eclipse RCP 中的右键单击文件?

windows - 延长 Windows 应用程序的证书过期日期

batch-file - 尝试从批处理文件运行VBS MsgBox

http - 在 Windows 7 上以编程方式清除 Internet Explorer 8 中的缓存

css - 找不到 postcss 导入 iconfont .woff .ttf

JavaScript : How to get root directory from a path

windows - 在 Windows 和 OS X 中设置 FPU

java - 空 LibGdx 游戏循环泄漏内存

windows - Eclipse 3.5 64 位性能 Windows 7