Saturday, December 15, 2007

DOS Prompt FOR command

The FOR command available in the DOS prompt is surprisingly useful. As you may have seen in the earlier blog the help on the FOR command (i.e. "HELP FOR") has a lot of useful information. I personally find that I use the FOR /L option more than any of the others. For example, if you need to create 100 directories named NewDir0 to NewDir99 you clearly do not want to do that from the Windows Explorer (AKA Windows Exploder). And you do not want to start typing mkdir manually from the command prompt either.

Instead, use the FOR command. But before you start creating a hundred directories, you should always use the echo command to test your FOR loop.

The FOR /L allows you to specify the start, step, and end of the loop, so the command:
C:\>FOR /L %i in (0,1,99) do @echo NewDir%i

Will echo:
NewDir0
NewDir1
...
NewDir99

Once it works the way you want it to, simply replace echo with your desired command (e.g. mkdir) and you whoopty you got yourself 100 directories.

Nifty

No comments: