ninja
ninja

Reputation: 29

How can i write a for loop in bat and execute a command on each iteration

I want to accomplish the following in a bat script

I want to create an array, in which the elements are separated by space, for instance:

array = host1 host2 host3

And run a for loop on that array where each element is passed as a parameter to a command for instance: psexec //host1 cmd likewise all the server names should be passed as an argument.

How can I achieve this?

Upvotes: 0

Views: 437

Answers (2)

Alex Lucaci
Alex Lucaci

Reputation: 630

You can try it with a list like below:

@echo off 
set list=host1 host2 host3
(for %%a in (%list%) do ( 
   ;dosomething with %%a;
))

Please be aware when setting the list and do not put any spaces before and after =

Upvotes: 2

Stephan
Stephan

Reputation: 56180

for %%a in (%array%) do echo %%a

for use directly on commandline, replace every %%a with %a.

Upvotes: 1

Related Questions