Barry
Barry

Reputation: 1665

batch file for loop

I have a performance run scenario which calls

cd %AUTORUN_DIR%
call  abc.bat 

cd %AUTORUN_DIR%
call  abc.bat 

cd %AUTORUN_DIR%
call  abc.bat 

This currently runs for one Operator. Now i want to edit this and run this code for 3 operators (Operator1 , Operator2,Operator3)

so i want something like this

set j = 1;

set operator = "Operator"%j% (Expecting this to be Operator1 in the first run of the loop)


for operator in ("Operator1","Operator2","Operator3") do

cd %AUTORUN_DIR%
call  abc.bat 

cd %AUTORUN_DIR%
call  abc.bat 

cd %AUTORUN_DIR%
call  abc.bat 

I want this to run for Operator1,Operator2,Operator3.

With my limited batch skills , i am finding it difficult to do.

Please help

Upvotes: 1

Views: 367

Answers (2)

Andriy M
Andriy M

Reputation: 77737

If your names have a fixed pattern (like in your example: operator1, operator2 etc.), you could use a FOR /L loop:

FOR /L %%o IN (1,1,3) DO (
  SET operator=operator%%o

  cd %AUTORUN_DIR%
  call  abc.bat 

  cd %AUTORUN_DIR%
  call  abc.bat 

  cd %AUTORUN_DIR%
  call  abc.bat 
)

Still, when there are just a few names, I would probably go with @Aacini's suggestion, as it is simple, clear and straightforward. (It is also flexible, because it allows you to use arbitrary names and specify/process them in an arbitrary order.)

Upvotes: 1

Aacini
Aacini

Reputation: 67266

You may define a subroutine and pass to it the current operator in the subroutine parameter, and then define the operator variable with the parameter value:

for %%o in ("Operator1","Operator2","Operator3") do call :theProcess %%o
goto :EOF

:theProcess

rem For example:
echo Current operator is: %1

set operator=%1

cd %AUTORUN_DIR%
call  abc.bat

cd %AUTORUN_DIR%
call  abc.bat

cd %AUTORUN_DIR%
call  abc.bat

exit /B

Please, feel free to do any question about this code.

Upvotes: 2

Related Questions