windows 目录名称中有空格的循环批处理文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5553040/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 08:14:55  来源:igfitidea点击:

batch file for loop with spaces in dir name

windowsbatch-filecmd

提问by Andrew Bullock

How do I modify this:

我该如何修改:

for /f %%a IN ('dir /b /s build\release\*.dll') do echo "%%a"

to work when the path contains spaces?

当路径包含空格时工作?

For example, if this is run from

例如,如果这是从

c:\my folder with spaces

it will echo:

它会回声:

c:\my

Thanks

谢谢

回答by Jan Zyka

You need to use:

您需要使用:

for /f "delims=" %%a IN ('dir /b /s build\release\*.dll') do echo "%%a"

for /f "delims=" %%a IN ('dir /b /s build\release\*.dll') do echo "%%a"

This overrides the default delimiters which are TAB and SPACE

这将覆盖 TAB 和 SPACE 的默认分隔符

回答by Jason

I got around this by prepending "type" and putting double quotes surrounding the path in the IN clause

我通过在 IN 子句中添加“type”并在路径周围加上双引号来解决这个问题

FOR /F %%A IN ('type "c:\A Path With Spaces\A File.txt"') DO (
    ECHO %%A
)

This articlegave me the idea to use "type" in the IN clause.

这篇文章给了我在 IN 子句中使用“类型”的想法。

回答by thomspengler

If you don't want to deal with "quotes" you can use the "s" switch in %~dpnx[]... this will output the short filenames that are easy to work with.

如果您不想处理“引号”,您可以使用 %~dpnx[] 中的“s”开关...这将输出易于使用的短文件名。

from the Command line...

从命令行...

for /f "delims=" %f IN ('dir /b /s "C:\Program Files\*.dll"') do echo %~sdpnxf

inside a .CMD/.BAT file you need to "escape" the [%] e.g., double-up [%%]

在 .CMD/.BAT 文件中,您需要“转义” [%] 例如,加倍 [%%]

for /f "delims=" %%f IN ('dir /b /s "C:\Program Files\*.dll"') do echo %%~sdpnxf