Batch File ReadMe


You should already know about the /? "help" option that every DOS command responds to. Here's where to find more help.

Windows XP Batch Help:  Go to "Start", then "Help and Support", then under the "Pick a Task" section, select the "Tools" link. At the very bottom of the Tools list, you'll find three entries that will help you with your command-line batch questions. The best thing there is the "Command-line reference A-Z". Be sure to read about:
For
Set
Redirection operators
If you have an OEM version of Windows (Like Dell, where they replaced the Help section with something else), you may need to read the XP Command-line reference A-Z on the web.
In my opinion, the Tools "Command shell overview" entry is too advanced for a beginner, but if you've already got some batch experience, this might be what you need.  If you don't have "Help and Support" in your Start menu, right-click the Start button and select "Properties", then  on the "Start Menu" tab, select the "Start menu" option and click the "Customize" button.  Go to the "Advanced" tab, and select "Help and Support" on the "Start menu items:" list.

Windows 2000 Batch Help:  Go to "Start", then "Help", then "Reference", then "MS-DOS Commands". No kidding! Do it now! Find the help. Read the help. The things you need to learn the most are the two entries for these :
For
Replaceable parameter

Be sure to follow the "For" link for the "additional forms of the for command". Also get a command prompt and type this:
set /?
For some reason, the "Help" file has zero good info on the "set" command. You have to get the command prompt "/?" help to get the good stuff.  If you want extra credit, you can read the sections on:
Conditional processing symbols
Redirection

Those items aren't needed too often, but they can help you to do things in a cleaner or more structured way.

Windows 95/98 Batch Help:   You may wonder why you can't find DOS command help by using the Help icon on the Start menu. I wonder too. But if you get the missing "OldDos" commands, you'll get the HELP command. You can find the OldDos commands on your Windows 9x CDROM in the \Other\Oldmsdos directory or from:
ftp://ftp.microsoft.com/Products/Windows/Windows95/CDRomExtras/OtherUtilities/olddos.exe
ftp://ftp.microsoft.com/softlib/mslfiles/olddos.exe
You should know the "Help.com" command requires use of the "Qbasic.exe" and "Help.hlp" files as well. Other useful DOS commands are winset.exe and shortcut.exe. Winset lets you set environment variables globally (they persist once your batch file ends). Shortcut lets you make and modify shortcuts from the command line. You'll find both of these together as "envars.exe" on your CDROM under \Admin\Apptools\Envvars or from Microsoft at:
ftp://ftp.microsoft.com/Products/Windows/Windows95/CDRomExtras/AdministrationTools/ApplicationTools/envars.exe

No Matter What You Use, the "FOR" command has enough power that it can solve ninety percent of all your problems. Really! Reading the entry on the FOR command should take you about a half hour. While reading, you'll confirm your suspicion (around the time you are reading the difference between single quotes, double quotes, and back quotes) that Bill Gates is mad. The stuff on the "set" command and the "parameters" will be much easier to understand. Those two (along with "for") will solve 99% of all your batch problems. Just don't bother trying to memorize it. Just read all of it so you know what it can do. You can always look up the details later when you need to actually use it.


The |<>@ symbols

Use the "pipe" character "|" (the vertical bar) to send the output from a command into the input of another command. For example:
type test.txt | program.exe
That would send the output of the "type" command into the input of the "program.exe" command. The "type" command in this case would be putting out the contents of the file "test.txt". The "program.exe" would (in theory) accept that as it's input instead of accepting input from the keyboard. Use redirection characters ">" and "<" to send output between files and programs. Notice the difference? The pipe sends stuff between two PROGRAMS. Redirection is between a program and a FILE. The redirection arrow lets you know what direction the data is flowing. For example:
program.exe > test.txt
would take the output of "program.exe" and put it in the file "test.txt" INSTEAD of displaying it on the screen. The data flows out of the program "program.exe" and into the file "test.txt". On the other hand:
program.exe < test.txt
Would cause "program.exe" to use "test.txt" as it's input INSTEAD of taking input from the keyboard. The data flows out of the file "test.txt" into the program "program.exe". So these two lines are different ways of doing the same thing:
type test.txt | program.exe
program.exe < test.txt
They both end up telling "program.exe" to use the data in "test.txt" for input instead of using the keyboard. The difference between ">" and ">>" is that ">"
normally creates a new file, replacing what was there, while ">>" just adds to the end of the file (If the file doesn't already exist, it will be created). You can even use redirection in non-intuitive order and it still works. For example, these two lines do the same thing:
program.exe > test.txt
> test.txt program.exe
Why do it the second way? Sometimes the second way looks neater when you have lots of  program commands going into a single file.
The "@" symbol can be put on the beginning of any command to stop the command from appearing on the screen. Any output from the program goes to the screen, but the command itself doesn't. For example, on a "dir" command, I only want to see a list of files. I do NOT want to see the command "dir".  Normally, you can turn off all screen echoes by using the "echo off" command. So anything after the "echo off" command only shows program output. Unfortunately, the echo command still gets echoed! However, if you put an @ sign in front of the echo command, it turns off the echo from the echo command. That's why most batch files start with this:
@echo off


Writing your first batch program
You'd be surprised how many people are in that narrow transition period of knowing how to type commands, but not knowing how to put them together in a batch file. Here is the short version: Get yourself a DOS prompt. Type in the commands you need to do whatever it is you need to do. If your commands work, open up Notepad and type those SAME COMMANDS in the SAME ORDER. Don't type what appeared on the screen, just type what you actually typed in. Save that file with a bat extension ( For example "test.bat"). Now instead of having to type the commands, you can just double-click the batch file. Sure, your first batch file may only have two or three commands, but it counts. It's a batch program.
 

Lost? Look at the site map.

Bad links? Questions? Send me mail.

Google
Yahoo
Ask Jeeves