我有一个程序的长路径名,我必须在for/f循环中运行,其中包括一个右括号")",我需要从中解析输出:
for /f "tokens=1" %%G in ('"C:\Documents and Settings\myaccount\Desktop\Test_release (x86)\program.exe" list') do (echo Will do something with %%G)
Run Code Online (Sandbox Code Playgroud)
...'list'是传递给我程序的参数.我收到错误"'C:\ Documents'不被识别为内部或外部命令,可操作程序或批处理文件."
我知道问题是,右括号实际上关闭了"for"块,因此结尾的双引号不是"看到"的,所以长路径名不再包含在双引号内.我不明白的是为什么会发生这种情况,因为我的路径是用双引号括起来的?我也尝试了usebackq选项:
for /f "usebackq tokens=1" %%G in (`"C:\Documents and Settings\myaccount\Desktop\Test_release (x86)\program.exe" list`) do (echo Will do something with %%G)
Run Code Online (Sandbox Code Playgroud)
......没有更好的结果.我试图逃避这样的"^)"或像这样的"^^)",无所事事.尝试加倍双引号:
for /f "tokens=1" %%G in ('""C:\Documents and Settings\myaccount\Desktop\Test_release (x86)\program.exe"" list') do (echo Will do something with %%G)
Run Code Online (Sandbox Code Playgroud)
仍然无法正常工作.
此外,我实际上使用了一个保存路径的变量,这个变量事先不知道(从%CD%构建),并且激活了EnableDelayedExpansion.我尝试了延迟扩展(在其他情况下修复了类似的问题)以防止变量在读取时扩展并在执行时延迟它:
setlocal EnableDelayedExpansion
set _var=%CD%\program.exe
@REM _var now contains C:\Documents and Settings\myaccount\Desktop\Test_release (x86)\program.exe
for /f "tokens=1" %%G …
Run Code Online (Sandbox Code Playgroud)