The FOR command is one of the most important commands you can use as a Windows administrator. I have a short article on the command you can download at http://www.jdhitsolutions.com/tutorials.htm (grab the FOR Essentials link) .
ManageEngine ADManager Plus - Download Free Trial
Exclusive offer on ADManager Plus for US and UK regions. Claim now!
Here are few of the examples I used in the Commandline Script session. (By the way, if you'd like to see more Command line stuff at future Techmentor shows or other scripting topics, be sure to let me or Don know.)
Assuming a text list of computer names, servers.txt and you want to do something to each computer, like ping it, you could run:
for /f %i in (servers.txt) do @ping %i
If you wanted to run cacls on a set of local directories you might try something like this:
for /f "tokens=*" %i in ('dir /b /ad') do @cacls %i
I use tokens to handle any directory names with spaces. If you wanted to send the results to a text file, use simple redirection. But use >> otherwise you'll only get the results from the last directory.
for /f "tokens=*" %i in ('dir /b /ad') do @cacls %i >>results.txt
One trick I use to do this for remote machines is to map a network drive to a file share then run cacls on the mapped drive.
Technorati Tags:
commandline
Scripting
You know with hindsight i wish i had mapped a remote drive to the local drive I was on when running cacls on a subdirectory on a server.
I was doing something along the lines of
for /D %i in (*) do cacls %i /r:everyone
Now I was in the c:\documents and settings directory (and had a screenshot to prove it) but when I ran that command for some reason it decided to run it on c:\winnt….the end result really was not pretty.
If I had mapped a drive to c:\documents and settings I would not have affected the rest of the server.
BTW – the blog is excellent and very useful.