Santhosh Pai
Santhosh Pai

Reputation: 2625

Running an infinite loop in multiple command prompts using batch script

I'm trying to write an batch script to run an infinite loop in 10 different command prompts ,but that doesn't seem to work fine .It opens the command prompts and disappears ,i have posted the script below please let me know where exactly is the problem.

 for /l %%x in (1, 1, 10) do (

start cmd /c ":up echo loop && goto up"

 )

Upvotes: 3

Views: 10776

Answers (4)

LS_ᴅᴇᴠ
LS_ᴅᴇᴠ

Reputation: 11151

Your batch is not working because labels require new line before commands, and you don't even have a & after :up. I would make it this way:

If Not %1.==. GoTo :Up
For /L %%i In (1,1,10) Do Start "%~nx0 %%i" Cmd /C %0 %%i
GoTo :EOF

:Up
Echo %1
GoTo :Up

Upvotes: 2

Endoro
Endoro

Reputation: 37569

for /l %%a in (0) do start "%~f0"

Upvotes: 4

Joris Claassen
Joris Claassen

Reputation: 56

I think two issues block the functionality (you want).

  1. By using echo all the remaining of the line will be printed (even if you get the loop working).
  2. Working with labels requires the use of script file(s). You can't use labels in the console. (I mean interactively: try it, enter a :label, enter some other lines and than enter goto label. Then you won't get into a loop!)

When I use for caller.cmd:

for /l %%x in (1, 1, 10) do (
    start cmd /c test.cmd
)

and for test.cmd:

:up 
echo loop 
goto up

it works (at least for me ...)

Even when I use for test.cmd:

:up 
echo loop && goto up

it works. But than each console will show loop && goto up in stead of loop!

While I was writing this down, Martyn had already given a likewise answer. Maybe that's convincing enough for you.

Upvotes: 3

SmithMart
SmithMart

Reputation: 2801

I would do this in 2 different batch files.

1 file is the start up, and opens the 2nd batch 10 times.

in the 2nd batch you would have whatever work you want done.

Batch 1 (start.bat):

 for /l %%x in (1, 1, 10) do (

 start "loop" loop.bat

 )

 pause

Batch 2(loop.bat):

REM Whatever work you want to be looping, for testing I have left it as a pause.
pause

then just run start.bat and you will see it open 10 new command windows all with the pause command.

Martyn

Upvotes: 2

Related Questions