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
Showing posts with label FOR command. Show all posts
Showing posts with label FOR command. Show all posts
Saturday, December 15, 2007
DOS prompt file information
Ever need to get the size of a file or get the short DOS 8.3 name from the command line or from a BAT script. Simply type in "HELP FOR" in the DOS prompt to get help on the FOR command. In here you'll also find the nifty tags to get that special information about files passed as arguments. For example, %~s1 will get the short DOS 8.3 name of the first argument, %~z1 will get the file size, %~p1 will get the path, etc.
You may for example create a small BAT file (e.g. named dos83.bat) that contains the following line. When called it will show the short DOS8.3 name of the argument passed in.
@echo %~s1
Output Example:
U:\My Documents\My Templates>dos83 .
U:\MYDOCU~1\MYTEMP~1
Nifty
You may for example create a small BAT file (e.g. named dos83.bat) that contains the following line. When called it will show the short DOS8.3 name of the argument passed in.
@echo %~s1
Output Example:
U:\My Documents\My Templates>dos83 .
U:\MYDOCU~1\MYTEMP~1
Nifty
Subscribe to:
Posts (Atom)
