鉴于以下Windows批处理(.cmd)文件,
@echo off
call :Test arg1; arg2
exit /B
:Test
echo %1
echo %2
exit /B
显示:
arg1
arg2
如何将分号作为文字传递,以便显示:
arg1;
arg2
非常感谢
更新:
好的,为了让事情更清楚,我真的想做这样的事情:
@echo off
call :Execute "c:\Program Files (x86)\Whatever\Test.exe" arg1 arg2
exit /B
:Execute
echo %1 %2 %3 %4 %5 %6 %7 %8 %9
%1 %2 %3 %4 %5 %6 %7 %8 %9
exit /B
因此,只有我想要回显的命令才会被传递给"执行" (可变数量的args)。但是,这些args可能包含像分号这样的字符,所以它们需要按字面意思传递。
答案 0 :(得分:2)
查看此批次的输出:
@echo off
call :Execute "c:\Program Files (x86)\Whatever\Test.exe" arg1; arg2
exit /B
:Execute
echo * %*
echo - %1 %2 %3 %4 %5 %6 %7 %8 %9
%*
exit /B
输出
* "c:\Program Files (x86)\Whatever\Test.exe" arg1; arg2
- "c:\Program Files (x86)\Whatever\Test.exe" arg1 arg2
答案 1 :(得分:1)
分号;
构成一个标记分隔符,就像 SPACE , TAB ,逗号,
,等号{ {1}}和非中断空格(代码=
)。如果希望它们按字面意思包含在命令行参数中,则需要将参数括在引号0xFF
中:
""
输出将是:
@echo off call :SUB semicolon;lost "some spaces " "semicolon;kept", no,comma "a,comma" exit /B :SUB echo ORIGINAL ARGUMENTS: echo(%* echo PARSED ARGUMENTS (EACH ENCLOSED IN ""): :LOOP if "%~1"=="" goto :EOF echo("%~1" shift /1 goto :LOOP
答案 2 :(得分:0)
您可以在引号中传递分号并稍后删除引号:
@echo off
call :Test "arg1;" arg2
exit /B
:Test
echo %~1
echo %2
exit /B
打印参数不变的替代方法:
@echo off
call :Test arg1; arg2
exit /B
:Test
FOR /F %%I IN ("%*") DO echo %%I
exit /B