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) .
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.