FOR loop question

  • Thread starter Thread starter Mike Celone
  • Start date Start date
M

Mike Celone

I have a batch file that uses a FOR loop to read workstation names from a
text file and executes a series of commands. Right now it uses 5 seperate
FOR loops to read the same file and execute the other commands. The first
one creates the directory on the machine, the 2nd copies the files to the
machines, 3rd runs the program, 4th deletes the files, and the 5th reboots
the machine. Here's the batch file:

FOR /F "delims=\, " %%j in (F:\iseries\workstations.txt) do md
\\%%j\c$\temp\iseries
FOR /F "delims=\, " %%i in (F:\iseries\workstations.txt) do copy
\\server\iseries\*.* \\%%i\c$\temp\iseries
FOR /F "delims=\, " %%g in (F:\iseries\workstations.txt) do psexec \\%%g
c:\temp\iseries\setup.exe
FOR /F "delims=\, " %%h in (F:\iseries\workstations.txt) do del
\\%%h\c$\temp\iseries /q
FOR /F "delims=\, " %%k in (F:\iseries\workstations.txt) do shutdown \\%%k
/r /t:0 /c

Right now it creates the directory on all the machines and then moves onto
the next FOR loop. I would like instead to have one FOR loop but execute
all 5 commands then move onto the next machine in the file. Is this
possible? I normally do vbscript but I need to use a batch file for this
instead. Thanks for any help!

Mike
 
Just figured it out. I created a seperate batch file with the commands in
it and I just pass the %%j variable to it. Works great!

Mike
 
Mike Celone said:
I have a batch file that uses a FOR loop to read workstation names
from a text file and executes a series of commands. Right now it uses
5 seperate FOR loops to read the same file and execute the other
commands. The first one creates the directory on the machine, the 2nd
copies the files to the machines, 3rd runs the program, 4th deletes
the files, and the 5th reboots the machine. Here's the batch file:

FOR /F "delims=\, " %%j in (F:\iseries\workstations.txt) do md
\\%%j\c$\temp\iseries
FOR /F "delims=\, " %%i in (F:\iseries\workstations.txt) do copy
\\server\iseries\*.* \\%%i\c$\temp\iseries
FOR /F "delims=\, " %%g in (F:\iseries\workstations.txt) do psexec \\%%g
c:\temp\iseries\setup.exe
FOR /F "delims=\, " %%h in (F:\iseries\workstations.txt) do del
\\%%h\c$\temp\iseries /q
FOR /F "delims=\, " %%k in (F:\iseries\workstations.txt) do shutdown \\%%k
/r /t:0 /c

Right now it creates the directory on all the machines and then moves onto
the next FOR loop. I would like instead to have one FOR loop but execute
all 5 commands then move onto the next machine in the file. Is this
possible? I normally do vbscript but I need to use a batch file for this
instead. Thanks for any help!

Mike

FOR /F "delims=\, " %%j in (F:\iseries\workstations.txt) do (
md \\%%j\c$\temp\iseries
copy \\server\iseries\*.* \\%%j\c$\temp\iseries
psexec \\%%j c:\temp\iseries\setup.exe
del \\%%j\c$\temp\iseries /q
shutdown \\%%j /r /t:0 /c
)

Why don't you instll from a central share to avoid copying/deleting ?
You could also call a sub from the for loop.

hth
 
Thanks for the tip. I'm using PSEXEC to execute the setup program remotely
on each machine. Because it is running in the localsystem context it has no
network rights and can't access any shares.

Mike
 
You can also put that second batch file into the first by
instituting a call to a label as a subroutine
(WinNT/2000/XP), something like this ...

for /f %%a in ('input.txt') do call :sub %%a
goto :EOF

:Sub
:: Your secondary batch process goes here ...
echo For example: %1

Note that :EOF is a predefined (system) label that means
end-of-file.

Tom Lavedas
===========
 
Back
Top