我有一个batch fileMyTest.bat
MyTest.bat call "first.bat" call "second.bat" call "third.bat"
现在执行MyTest.bat时,我会传递逗号分隔的参数
调用MyTest.bat first.bat,second.bat
现在里面MyTest.bat我想检查哪些parameter passing和基于那些使用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?
下一个脚本需要另一个参数顺序(批量名称列表,以结束所有其他参数):
@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.bat
输出 (显示使用分隔符的独立性):
==> 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
另一种方法:第一个参数=用双引号括起来的逗号分隔批名称列表:
@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
。
资源 (必读,不完整):
%~G
, %~1
等特殊页面) 命令行参数(参数) %variable:StrToFind=NewStr%
等) 变量编辑/替换