Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Howto use FOR to read list of pc names & perform command on each? 3

Status
Not open for further replies.

GeneralDzur

Technical User
Jan 10, 2005
204
US
I'm using a script to defragment a list of computers remotely. Right now, my script is a long list of "psexec" commands that start defrag.exe on each computer.

But I'd like for the script to read a list of computer names in a text file, instead of me having to manually make a list inside of the script.

Currently:

Code:
psexec -a 0 -belownormal -d \\computer1 defrag c: -f
psexec -a 0 -belownormal -d \\computer2 defrag c: -f
psexec -a 0 -belownormal -d \\computer3 defrag c: -f
<etc>...

how can I have it read the list of computer names one-by-one from a text file??

I.E.

Code:
for each %COMPUTERNAME% in %LIST_OF_NAMES%
psexec -a 0 -belownormal -d \\%COMPUTERNAME% defrag c: -f

???
 
Create a text file with a list of computer names, one per line. In the below code I used filename D:\test.txt.
Then create a cmd file with the below line and run it.
Code:
FOR /F %%i in (D:\test.txt) do psexec -a 0 -belownormal -d \\%%i defrag c: -f
 
This is great, thanks a lot. Are the parentheses necessary around the file name?

Can you explain the logic here, how the %%i gets filled in with the file names?
 
Parentheses are necessary around any set defined in a FOR command. Open a command prompt and enter FOR /? . You will see that the FOR command is quite powerful.
 
This is great Freestone, thanks very much for the help.
 
For what it's worth, as much as I like using FOR loops myself, psexec has this capability built in. It can take a direct list of multiple machines as paramters or the @file parameter for a text file with the list. Try psexec /?
 
I wondered where you were smah as usually you answer batch file type questions :) Thank you for the heads-up.
 
I didn't know about that, it would probably be simpler to use it's built-in function then :p

I ended up using that FOR loop for about four other scripts, to remotely defrag, remotely delete cached profiles older than 45 days, and remotely change some registry entries. It was a big help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top