简体   繁体   English

批处理文件for循环,在dir名称中包含空格

[英]batch file for loop with spaces in dir name

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 谢谢

You need to use: 你需要使用:

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

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 article gave me the idea to use "type" in the IN clause. 本文给了我在IN子句中使用“type”的想法。

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 [%] eg, double-up [%%] 在.CMD / .BAT文件中你需要“逃避”[%],例如,双倍[%%]

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM