Guides:Shells/COMMAND.COM-CMD.EXE/Batch Files/Basics

From CoderGuide

Jump to: navigation, search


A batch file (*.bat) is a shell script run by the only command interpreter (COMMAND.COM) that DOS, as well as the earlier versions of windows, except NT based versions (NT, 2000+, and all versions after XP). CMD.EXE, an extended version of COMMAND.COM, also runs BAT files.

All batch files are ASCII text (plain text). The Windows text editor, Notepad, DOS 5.0's EDIT, and the earlier DOS EDLIN programs only create ASCII text files. You can also tell Wordpad, WordPerfect, Microsoft Word, and Write to save a text document as a plain text. To do this, click on File, then '"Save As, and select ASCII/plain text from the drop-down list.

Ever since Windows 95, Windows hides the file extensions of known types, and thus prevents you from changing file name extensions. To learn how to fix this horrific security fault in windows(and find out why this "feature" is bad), see [Guides:OS/Windows/Configuring Windows for Power Users].

Creating a batch file

To begin with, start up your favorite text editor. I recommend starting COMMAND.COM or CMD.EXE, And then type in EDIT to load the simple text editor. EDIT, despite being old, is easier on the eyes, and bypasses all the funky behavior of windows when trying to open, edit, and save text files that don't have .txt extensions.

EDIT will recognize mouse movements; however, the keys are different for the DOS and Windows XP versions of Edit. (The Windows command line version uses the standard key combinations for cutting, copying, and pasting text.)

So, at the command prompt, type:

edit test.bat

Then enter the following text:

echo Hello World!

Now hit Alt-F, or click on File, save, then hit Alt-X to exit. And run your batch file.

C> test
C> echo Hello World!
Hello World!

Note: When running files, DOS (and the windows command prompt) first look for .COM files, then .EXE, and finally .BAT. That means if there is a file "TEST.EXE" or "TEST.COM", then those programs will be run instead of your TEST.BAT. If that happens, or if you want to make sure the .BAT file runs instead of any other on the disk, then type the extension too (in other words, type "test.bat" instead of "test").

Right now our batch file is showing every command as if we typed them. Normally we wouldn't want to see each command like that. We can prevent this by using the statement echo off. So edit your batch file again and add that statement to the top so our new file looks like:

echo off
echo Hello World!

Now lets see what happens:

C> test
C> echo off
Hello World!

Still not quite what we want... But there is a solution! If we precede a command in a batch file with the at sign, then that command won't be displayed regardless of whether or not echo is off. So make that change to keep echo off from appearing:

@echo off
echo Hello World!

And run it again:

C> test
Hello World!

Which is what we want to see.

Paramerers and Variables

To make a batch file more useful, we will want it to be able to use parameters. A Batch file can take up to 9 parameters. We access each one by putting a percent sign in front of a number. We access environmental variables the same way. Single character variables only need the one percent sign, but multi-letter variable names need to be enclosed in percent signs.

Here is an example:

@echo off
echo parameter one is %1
echo parameter two is %2
set var1=Hello
echo var1 is %var1%
set var1=%1
echo var1 is %var1%

Save the file as test2.bat and run:

C>  test2 para1 para2 para3 para4
parameter one is para1
parameter two is para2
var1 is Hello 
var1 is para1

Now that we've covered the basics, we will next discuss flow control.

Personal tools