批处理文件 for 循环,目录名称中包含空格
如何修改它:
for /f %%a IN ('dir /b /s build\release\*.dll') do echo "%%a"
在路径包含空格时起作用?
例如,如果从中运行它将
c:\my folder with spaces
回显:
c:\my
谢谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要使用:
for /f "delims=" %%a IN ('dir /b /s build\release\*.dll') do echo "%%a"
这会覆盖默认值分隔符为 TAB 和 SPACE
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
我通过在 IN 子句中添加“type”并在路径周围加上双引号来解决这个问题
这篇文章 给了我在 IN 子句中使用“type”的想法。
I got around this by prepending "type" and putting double quotes surrounding the path in the IN clause
This article gave me the idea to use "type" in the IN clause.
如果您不想处理“引号”,可以使用 %~dpnx[] 中的“s”开关...
这将输出易于使用的短文件名。
从命令行...
在 .CMD/.BAT 文件中,您需要“转义”[%],例如,加倍 [%%]
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.
from the Command line...
inside a .CMD/.BAT file you need to "escape" the [%] e.g., double-up [%%]
问题是字符串中有一个通配符被解释为文件名。带有空格的字符串还需要双引号。我不确定是否有办法摆脱通配符。
The problem is there's a wildcard in the string that gets interpreted as a filename. You also need double quotes for a string with spaces. I'm not sure if there's a way to escape the wildcard.