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

发布于 2024-10-30 15:26:18 字数 248 浏览 0 评论 0原文

如何修改它:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

半夏半凉 2024-11-06 15:26:18

您需要使用:

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

不如归去 2024-11-06 15:26:18

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

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

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

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

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.

眼泪都笑了 2024-11-06 15:26:18

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

从命令行...

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

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

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

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

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 [%%]

for /f "delims=" %%f IN ('dir /b /s "C:\Program Files\*.dll"') do echo %%~sdpnxf
偏闹i 2024-11-06 15:26:18

问题是字符串中有一个通配符被解释为文件名。带有空格的字符串还需要双引号。我不确定是否有办法摆脱通配符。

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

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.

for %a IN ("dir /b /s build\release\.dll") do echo %a
"dir /b /s build\release\.dll"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文