3

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"

 )

4 Answers 4

4
for /l %%a in (0) do start "%~f0"
Sign up to request clarification or add additional context in comments.

3 Comments

This i know , but i want multiple instances of it opened once
This was about to crash my system , but it opens many command prompts :)
The only infinite loop working frome console, not only from bat-file.
3

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.

Comments

2

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

Comments

2

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.