Q:

execute some code for each element of a list in batch

set machines=PC2 TEST-PC PC34
for %%a in (%machines%) do (
   echo %%a 
   echo. 
) > PCs.txt

I want to loop through a list of machines and execute code for each machine. 
But in my PCs.txt there is only one line with PC34.
What's my mistake? 
Thanks in advance!

A:

The loop is working properly; the only problem is that redirection to a file overwrites the file if it already exists. To append instead, use the >> redirection operator. (Clear the file beforehand to get rid of any existing content.) Like so:
set machines=PC2 TEST-PC PC34
copy /Y NUL: PCs.txt
for %%a in (%machines%) do (
   echo %%a 
   echo. 
) >> PCs.txt

