I have a test.cmd file with the following command:
call "cmd /c start echo foo && pause"
call "cmd /c start for /l %%x in (0, 1, 2) do python test.py config%%x"
The first command is working fine and shows that the general approach should work. The second one with the for loop gives me troubles.
When I run this command directly in a CMD window (with only one % sign before the iterator), it starts my python script "test.py" in a new CMD window 3 times in a loop as expected.
When I run the same command from my test.cmd (this time with two % of course), the new CMD window pops up and is gone right away. I don't get any error messages and can't get the new window to stay.
I suspect that I need to do some more encoding but I cannot figure out the correct syntax. What must I change to get this for loop to run from my test.cmd?
FOR /R - Loop through files (recurse subfolders) . FOR /D - Loop through several folders. FOR /L - Loop through a range of numbers. FOR /F - Loop through items in a text file.
Open multiple command prompts in Windows 10In the Windows taskbar, right-click the command prompt window icon and select Command Prompt. A second command prompt window is opened.
Right-click on the application and select "Run as administrator," then a new Command Prompt window opens.
To repeat one or more lines: Type R in the line command field of the line that is to be repeated. If you want to repeat the line more than once, type a number that is greater than 1 immediately after the R command. Press Enter.
if you insist on using call
, there is another level of parsing, so in a batchfile you have to use:
call "cmd /k start for /l %%%%i in (1,1,10) do echo %%%%i"
(replaced /c
with /k
to see the output)
try this:
call "cmd /c for /l %x in (0, 1, 2) do @python test.py config%x"
when you execute loops in command prompt you need single percentage symbol.
And you need to remove the start command after the cmd /c
because for
is internal cmd command. The @
is for suppress printing of prompt value on each iteration.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With