0

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?

ninja
  • 29
  • 7
  • 1
    What have you tried, and how has what you've tried failed? Ideally, you should provide a [Minimal, Complete, and Verifiable Example](https://stackoverflow.com/help/mcve) of what you've tried, and include specific information on how it failed, with error messages and/or erroneous output. SO is not a code-writing service; the best questions are those which provide useful information so that those who answer can guide you to devising your own correct answer. See [How to Ask a Good Question](https://stackoverflow.com/help/how-to-ask). – Jeff Zeitlin Jun 22 '17 at 12:39
  • 1
    Your data is not an _array_, but a _list_. Further details at [this answer](https://stackoverflow.com/questions/17605767/create-list-or-arrays-in-windows-batch/17606350#17606350) – Aacini Jun 22 '17 at 13:11

2 Answers2

2

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 =

Alex Lucaci
  • 620
  • 8
  • 18
1
for %%a in (%array%) do echo %%a

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

Stephan
  • 53,940
  • 10
  • 58
  • 91