I have a batch file. In which, I need to start another copy of itself in a new window with a parameter. I tried the command start "" "%~0" "Param"
but it said '"Param"' is not recognized as an internal or external command, operable program or batch file.
and didn't start anything. The only way I could get it to work was start %~0 Param
, but I figured that would crash if the path had spaces. So What is the correct way to start another instance of the current batch file with parameters?
-
Do you mean another additional instance, and the current stay running?– LotPingsCommented Jan 25, 2019 at 16:05
3 Answers
I would start a second cmd shell, something like:
start "" Cmd.exe %~0 parameters
just to give each iteration of the batch file its own command shell.
Note by OP: I had to do the following:
start cmd.exe /C %~0 parameters
-
Exactly what I was looking for, don���t know why I didn’t think of that. Thank you!! Commented Jan 25, 2019 at 16:11
-
Actually, this isn't working for me. I had to do start
cmd.exe /C %~0 parameters
Commented Feb 15, 2019 at 19:13
In order to call another batch file from a batch file, use call "name of script.bat"
or `start "name of script.bat"
Although you can do it without, unexpected results will happen, given that it will continuously call itself.
so technically, you can just write
%0 MyParam
To avoid an infinite loop, check if args are present:
:: Q:\Test\2019\01\25\SU_1298393.cmd
@Echo off
If "%~1" neq "" goto :HasArgs
Echo restart with parms
"%~0" "parms"
:HasArgs
Echo %0 started with %*
Pause
Exit /B
Sample run:
> SU_1298393.cmd
restart with parms
"SU_1298393.cmd" started with "parms"
Press any key to continue . . .
> SU_1298393.cmd foo bar
SU_1298393.cmd started with foo bar
Press any key to continue . . .