Syntax error trying to run batch files in loop
I want to run these 3 .bat files in loop. Tried these 2 codes and got invalid syntax error from both
@echo off
:loop
start "c:\1.bat" && "c:\2.bat" && "c:\3.bat" &&
GOTO :loopand
@echo off
for /l %%x in (1, 1, 9999) do ( start "c:\1.bat" && "c:\2.bat" && "c:\3.bat" && set /a loopCount=%loopCount%-1 if %loopCount%==0 GOTO:EOF
) 1 Answer
I believe too many && and should use a space before :EOF.
Personally, I'd avoid using && for future maintenance and readability.
If you want 1.bat, 2.bat, and 3.bat to remain on-screen, use /K shown below. Otherwise, use /C so 1.bat, 2.bat, and 3.bat can exit after running.
I'm not sure the purpose of %loopCount% -- other than a loop exit safeguard -- but be sure to initialize that variable ahead of using it (good coding practice).
Here's an example:
@echo off
set /a maxLoop=5
set /a loopCount=%maxLoop%
for /l %%x in (1, 1, %maxLoop%) do ( start %COMSPEC% /K c:\1.bat start %COMSPEC% /K c:\2.bat start %COMSPEC% /K c:\3.bat set /a loopCount=%loopCount%-1 if "%loopCount%"=="0" GOTO :EOF
) 5