Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open new cmd window and execute for-loop in it?

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?

like image 560
Demento Avatar asked Apr 17 '16 08:04

Demento


People also ask

How do I do a for loop in 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.

Can you Run 2 command prompts at once?

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.

How do I Run a new command prompt?

Right-click on the application and select "Run as administrator," then a new Command Prompt window opens.

How do you repeat a line in cmd?

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.


2 Answers

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)

like image 91
Stephan Avatar answered Oct 06 '22 00:10

Stephan


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.

like image 32
npocmaka Avatar answered Oct 06 '22 01:10

npocmaka