我有一个批处理文件MyTest.bat
MyTest.bat
call "first.bat"
call "second.bat"
call "third.bat"现在在执行MyTest.bat时,我将传递逗号分隔的参数,如
呼叫MyTest.bat first.bat,second.bat
现在,在MyTest.bat内部,我希望检查哪些参数被传递,并基于那些使用if else条件的参数。
我想执行内部声明。
例如,类似的东西
MyTest.bat first.bat,second.bat
now inside
I will check
get all parameters list param[] = {first.bat,second.bat}
if param[i] == "first.bat"
{
call "first.bat"
}
else if param[i] == "second.bat"
{
call "second.bat"
}
else if param[i] == "third.bat"
{
call "third.bat"
}//这可以确保只执行这些语句传递的参数,而不执行其他语句。
上面的代码只是一个伪代码,如何才能写出实际的MyTest.bat呢?
发布于 2015-11-27 16:22:19
下一个脚本需要另一个参数顺序(从批处理名称到结束所有其他参数的列表):
@ECHO OFF
SETLOCAL EnableExtensions
rem usage: 33955749.bat name address "first script", second, third
:loop
if "%~3" == "" goto :next
if exist "%~n3.bat" (
call "%~n3.bat" %1 %2
) else (
echo can't found file; failed call "%~n3.bat" %1 %2
)
shift /3
goto :loop
:next为了调试目的,准备示例文件"first script.bat"和second.bat;确保third.bat不存在:
==> >"first script.bat" echo @echo %~nx0 parameters: %%*=%*
==> >second.bat echo @echo %~nx0 parameters: %%*=%*
==> 2>NUL del third.batOutput (显示了与使用的定界符的独立性):
==> 33955749 name address "first script", second, third
first script.bat parameters: %*=name address
second.bat parameters: %*=name address
can't found file; failed call "third.bat" name address
==> 33955749 name address "first script" second; third
first script.bat parameters: %*=name address
second.bat parameters: %*=name address
can't found file; failed call "third.bat" name address另一种方法: fist参数=逗号分隔的批名称列表,其周围环绕着一对双引号:
@ECHO OFF
SETLOCAL EnableExtensions
rem usage: 33955749b "first script,second,third" name address
rem no spaces surrounding commas or names
rem wrong: 33955749b " first script , second, third" would fail
set "_names=%~1"
set "_names=%_names:,=","%"
rem debug echo _names="%_names%"
for %%G in ("%_names%") do (
if exist "%%~dpnG.bat" (
call "%%~dpnG.bat" %2 %3
) else (
echo can't found script; failed call "%%~dpnG.bat" %2 %3
)
)输出(显示对使用的分隔符的响应性):
==> 33955749b "first script,second,third" name address
first script.bat parameters: %*=name address
second.bat parameters: %*=name address
can't found script; failed call "D:\bat\SO\third.bat" name address
==> 33955749b "first script, second,third" name address
first script.bat parameters: %*=name address
can't found script; failed call "D:\bat\SO\ second.bat" name address
can't found script; failed call "D:\bat\SO\third.bat" name address注意,33955749.bat和33955749b.bat脚本都是
.bat。例如,33955749 name address first.cmd second.cmd会尝试调用first.bat和second.bat。
Resources (必需阅读,不完整):
%~G,%~1等特别网页) 命令行参数(参数)%variable:StrToFind=NewStr%等) 可变编辑/替换https://stackoverflow.com/questions/33955749
复制相似问题