Using the "for" Command in the Windows cmd shell

You can guarantee that the Windows command prompt will be available on every Windows server - you can't guarantee Power Shell will be there. Microsoft only started including Power Shell by default from Windows Server 2008R2 onward (Windows 7 for the desktop). Previous to that, Power Shell had to be installed before it would be available. If you have 500 Windows 2003 or 2008 (non-R2) servers on your network, it's not going to be a quick and easy task to install Power Shell on all of them, just so that they can execute your script.

That's where you might want to fall back to the reliable old command prompt. At least until those old, outdated servers have gone away.

The "FOR" command tends to be used in just about any useful cmd script. One of its most useful features is that it allows you to step through a list of text and perform an action on each item in that list. The list can come from anywhere - it could be the output of a command ("dir" for example) or it could be a list of servers you've gathered and placed into a text file. It can also disect the text and break it up into "tokens" based on delimiter characters, but here I'm going to cover just the basics, to get you started.

The command can be used in various modes, but I'm just going to cover my favorite here - file parsing mode. The basic command structure to read a file is:

for /F "tokens=*" %%I in (c:\yourfile.txt) do echo %%I

The command changes slightly to read the output of a command, in that you must enclose the command in single quotes ('):

for /F "tokens=*" %%I in ('dir /B') do echo %%I

%%I is the variable that will be set for each item in the list. Anything written after the "do" will be executed for every item in the list. Each time, %%I changes to the next item in the list.

Basic Example

Here's an example text file named c:\servernames.txt:


If we use the following command:

for /F "tokens=*" %%I in (c:\servernames.txt) do echo The Server Name Is: %%I

The output would be:

The Server Name Is: server01
The Server Name Is: server02
The Server Name Is: server03
The Server Name Is: server04

Hopefully you can already see the potential of this command. If you replace "echo" with a more useful command, you can do just about anything you want to each item in the list you have supplied.

Maybe you need to ping every server, and report back if any of them don't respond - well then replace the "echo" command with a "ping" command, and then write a little logic for what you'd like to return.

Working with files in a directory

Maybe you want to make a change to every file in a directory, such as adding a prefix. Here's an example:

Let's say we have a number of files in the c:\files directory. The files are named "importantfile1.txt", "importantfile2.txt" and "importantfile3.txt". We need to add a prefix to these to change the filenames to "really-importantfile1.txt", "really-importantfile2.txt" and "really-importantfile3.txt".

Here's how we could do it:
The output of the command "dir /B c:\files" will give us:


So, if we use this command, combined with the FOR command, we can execute a command on each item in the directory.

Here's the command we would use:

for /F "tokens=*" %%I in ('dir /B c:\files') do rename c:\files\%%I really-%%i

and the outcome would be that each file would be renamed from its original name, to "really-" plus its original name. A dir /B on the directory would now output:


Obviously you could have renamed 3 files very quickly, manually - this is just for the example. - but what if there are 50,000 user files that need to be modified in some way - this will perform the task in seconds... without any outside tools, and just a single line of code.