Reputation: 16219
I have one batch file MyTest.bat
MyTest.bat
call "first.bat"
call "second.bat"
call "third.bat"
Now while executing MyTest.bat I will pass comma separated parameters like
call MyTest.bat first.bat,second.bat
now inside MyTest.bat I want to check which parameters are passed and based on those using if else condition
I want to execute internal statements.
for example something like it
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"
}
// this assures that what parameters I passed only those statements gets executed not other.
The above code is just a pseudo code how can write actual MyTest.bat?
Upvotes: 0
Views: 2539
Reputation: 30103
Next script requires another parameter order (list of batch names down to end all other parameters):
@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
For debugging purposes, prepare sample files "first script.bat"
and second.bat
; ensure that third.bat
does not exist:
==> >"first script.bat" echo @echo %~nx0 parameters: %%*=%*
==> >second.bat echo @echo %~nx0 parameters: %%*=%*
==> 2>NUL del third.bat
Output (shows independency on used delimiters):
==> 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
Another approach: fist parameter = list of comma-separated batch names surrounded with a pair of double quotes:
@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
)
)
Output (shows responsivity to used delimiters):
==> 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
Note that both 33955749.bat
and 33955749b.bat
scripts
.bat
.For instance, 33955749 name address first.cmd second.cmd
would attempt to call first.bat
and second.bat
.
Resources (required reading, incomplete):
%~G
, %~1
etc. special page) Command Line arguments (Parameters)%variable:StrToFind=NewStr%
etc.) Variable Edit/ReplaceUpvotes: 1