如何成功回复For循环到批处理脚本?

时间:2015-02-12 19:52:11

标签: windows batch-file

echo for /f "delims=" %%%i in ^(^'DIR /A -H /B^'^) do set "check^=^^!check^^!,"%%%i"" >> Uninstall.cmd

结果

for /f "delims=" %%i"" 

原始代码我试图回复到Uninstall.cmd

for /f "delims=" %%i in ('DIR /A -H /B') do set "check=!check!,"%%i""

任何人都可以帮我弄清楚如何让这行代码完全转义,以便将其输出到文件中吗?

1 个答案:

答案 0 :(得分:3)

如果您没有启用延迟扩展

echo for /f "delims=" %%%%i in ('DIR /A-H /B') do set "check=!check!,"%%%%i""

如果您启用了延迟扩展,则需要转义感叹号

echo for /f "delims=" %%%%i in ('DIR /A-H /B') do set "check=^!check^!,"%%%%i""

但是,如果代码在块内执行,则右括号也需要转义

if 1==1 (
    echo for /f "delims=" %%%%i in ('DIR /A-H /B'^) do set "check=!check!,"%%%%i""

    :: OR

    echo for /f "delims=" %%%%i in ('DIR /A-H /B'^) do set "check=^!check^!,"%%%%i""
)