BAT FILE PRIMER Chas Bornemann Sim Cyberworld Just what the heck are those goofy files that you see in install routines anyways? You know those ones that end in .bat? Simply, they are small text files that execute basic DOS commands and can call other programs in for execution. And of course they work wonders for writing install programs (which is the reason I'm writing this). ------------------ Lesson 1: NOW FOR DA BASICS: Time to write your first bat file ! Since I am basically a real dumb person I'll keep it simple for you as well. Here's the purpose of the first bat file: REM *** To create a vertical directory listing *** Oh oh? What's that rem all about? REM - is a keyword used to place a comment line in your bat file. During execution these lines are skipped. Thought you would kinda need to know that since that's gonna be the second line you write. One other command that is very basic to all BAT files us the ECHO command. This command controls what is and isn't displayed on the screen. By default DOS will display all commands in the bat file to the screen. That can be changed (and should be) using the ECHO command. Here is how to use it properly. REM *** This line makes sure the commands in your REM *** Bat file are not displayed to the screen. @echo Off REM *** This line will display something on the screen. echo. Hi Mom and Dad ! The period is necessary after the echo command btw ! ---------------------------------- LESSON 1 (con't) NOW LET'S WRITE A SMALL BAT FILE ! To write your very first BAT file let's make this fun. Let's use DOS EDIT. You can also use Notepad in Windows to do this. Here are the steps: #1 - At a DOS prompt type EDIT D.BAT. This will open the DOS EDITOR and save anything you write to a file called D.BAT. #2 - Ok, now we're in the editor and we can start to write our commands. On the first line type this: @echo off Now let's add some quicky comments to the file so we know what's going on later on when we come back to fix it or add something. #3 - Hit the carriage return (ENTER KEY) #4 - Type the following line. REM *** This program will let me display a vertical directory #5 - Hit the carriage return (ENTER KEY) (I always prefer to include a blank line between comments and code as it makes it much easier to read the program.) #6 - Type the command DIR /p #7 - SAVE THE file #8 - at the DOS prompt type D and hit enter Your bat file will now display the contents of that directory. Exciting stuff eh? --------------------------------------------- LESSON 2: CONTINUING WITH THE D.BAT FILE. Makin a menu Something that is needed in many bat files are menu's to allow us to do different things as the circumstances require. You will see several different COMMANDS introduced here. They are: CLS : CHOICE IF ERRORLEVEL GOTO CLS = Easy command to remember. This one clears the screen : - This is a character used by DOS (not a command) that signifies a program section. It is used with the GOTO command. GOTO - Jumps to another program section. Here is an example of the GOTO command in action. GOTO END Lines of code Lines of Code etc etc etc :END Note, the lines inbetween the GOTO statement and the :END statement are not executed. DOS knows to skip these lines of code. After the GOTO statement the next line that is executed is the :END statement (and usually that's the end of the program) ERRORLEVEL is a DOS keyword that works with the GOTO command when making choices. It's useage is very straight forward. IF ERRORLEVEL 1 GOTO END Guess what? If we had lines of code before the :END statement they would be skipped. So what's the difference between using just the GOTO statement or using it with the IF and ERRORLEVEL statements? The IF ERRORLEVEL statement checks to see whether a certain condition that you will enter in the computer is true. If it is then it will execute the GOTO. If it is not then it will continue with the next lines of code. CHOICE - This is the command that allows you to make a menu selection in a BAT file. It's usage is as follows: CHOICE /c:xyz /n Please choose x, y or z /c:xyz = The keys that are acceptable input. If the user hits another key the computer will beep /n = The choice statement will not display a prompt One other useful switch that can be used is: /s = makes the choice case sensitive Now it's time to make our menu ! If you remember from lesson 1 we created our bat file that looks just like the one below: ================================================= @echo off REM *** This program will let me display a vertical directory dir /p ================================================= Now it's time to make it do some more. Let's start by adding a menu to it. Using the DOS text editor from lesson 1 we can type the following code. Don't worry I've provided comments so you know exactly what's going on. If you don't feel free to ask me. ======================================================= REM *** MENU of Choices to choose which directory list REM *** you want to display or print :menu REM The above line indicates a place a GOTO statement can jump to. CLS REM Clears the screen echo. How do you wish to display the directory listing? echo. echo. echo. V = Vertical listing echo. W = Horizontal Listing echo. P = Print a vertical listing echo. X = Exit the program echo. echo. choice /c:vwpx /n Choose V, W, P or X REM Remember the echo command is used to display REM something to the screen? That is exactly what it REM is doing here. Displaying the menu on the screen. REM The CHOICE command is setting up the variables for REM the 4 options we have. It sets up the four keys REM V,W,P,X as the choices we can make. If we make REM another choice the computer beeps at us !!! CLS IF ERRORLEVEL 4 GOTO END IF ERRORLEVEL 3 GOTO PRINT IF ERRORLEVEL 2 GOTO WIDE IF ERRORLEVEL 1 GOTO VERTICAL REM Note how I use white space (blank lines). It simply REM makes the program easier to read later on by blocking REM certain sections of code and then including a blank REM line. REM The above section indicates where the program will REM jump to (GOTO). ERRORLEVEL is equal to the letter REM that we inputted at the keyboard from the menu. REM So ERRORLEVEL 4 = X, ERRORLEVEL 3 = P and so on. REM Remember the 4 choices were V, W, P, X (1-2-3-4). :VERTICAL REM Jump here is you chose V (ERRORLEVEL 1) DIR /P REM Display a vertical file listing to the screen. Make REM sure it only displays one page at a time. echo. pause echo. Press any key to continue REM Pause the program and continue it by pressing any key. REM The echo. command does create many choices for us. REM You may use any key to continue the program after the REM Pause command is given. I placed this command here so REM I would be able to see the last page of my directory REM listing. Otherwise the program jumps back to the REM MENU and the CLS command is carried out. Goto menu REM Go back to the menu. HINT - Look for the :menu line :wide dir /w/p echo. pause echo. Press any key to continue goto menu REM Hmmmmmmmmmmmmm, this looks just like the last section. REM It is except now we are telling DOS to display the dir REM directory with a wide listing one page at a time. Back REM to the menu we go. :print dir > prn goto menu REM Now what the heck is that > prn? You just want to REM confuse someone right? REM hehehehehe, yep REM Actually the > symbol is used to redirect the output REM away from the screen to another device. In this case REM I chose the printer (PRN) REM You can also get creative and use the > to redirect REM output to a file for example: REM dir > files.txt REM This line would create a file of your directory listing. REM Here's a line I use in a tracks directory that lets me REM make a directory listing for Marcello's dat/undat tool. REM dir/b > datinfo.txt REM Now the datinfo.txt is nearly complete to repack a track. REM Pretty slick eh? :END REM The last section of the program. The :END statement REM DOES NOT end the program. The bat file is ended when REM it has no more commands to execute. ======================================================== That's the program. Pretty neat stuff eh? In lesson 3 we will take a look at adding a form feed to the DIR statement, using the COPY CON and a few other little things. In lesson 4 we will look at variables again and also at calling other programs into the bat file and finally in lesson 5 we will tie it altogether by writing a bat file to install a track. Time for a break.