BAT FILE PRIMER Chas Bornemann Sim Cyberworld Hey, it's time to get back to the lesson book now. If ya want a review I'm just to lazy to write one here, so go back and review what I wrote in lessons 1 and 2. I did decide to throw some examples in a final lesson 6. So by the time we are done here with you will be able to write a installer for your track or for any other patch you created. ------------------ Lesson 3: A FEW TRICKS FOR YOUR BAT FILES: What is presented in this lesson is really not needed for writing install files for simulations. But the info is kinda neat and I thought you'd like to know how to do it. It evolves around adding the COPY CON statement. I am going to assume that you have Windows 95 or 98, a DOS Window and a text editor like Notepad to complete this. ADDING SOUND TO A BAT FILE: You can add sound to a bat file with a little editing practice. I prefer not to do it myself since I consider not necessary, but it's really a preference thing. Sound should be used carefully and only should be used to ALERT the user to a problem. A good place to use sound may be where an ERROR occurs in the processing of the bat file. Now exactly what kinda sound can ya git? You know that little bell? Well that's the sound ! Now how do ya git it? Here's the process. 1. In Windows, open a DOS Window. 2. Go to a temporary directory (best place to work) similar to c:\temp for your editing 3. At the command prompt (c:\temp>) type the following command COPY CON SOUND.BAT Copy Con is a DOS COMMAND that will create the file SOUND.bat You can create any file here. 4. At this point you will be on a blank line. Press the CTRL key and while you hold down the CTRL key press the G key. CTRL-G creates the SOUND you will hear !!! You will see ^G on the command line. 5. Press ENTER 6. Now you are at another blank line. Again press the CTRL key and while holding it down this time press the Z key ! This will exit the COPY CON routine. You will see ^Z on the command line followed by (1) files copied. 7. Your SOUND.BAT file is now complete. 8. At the DOS prompt type SOUND. The computer will BEEP at you. Great ! You know it works !!! You can close that darn DOS window for now (and they say DOS is dead) Now to put this command in your BAT file that you are using you will need to use a Windows text editor like Notepad. Open the SOUND.BAT file with Notepad (don't double click on it), instead right click on it and choose open Now all you will see is a single garbage character. That's what you want to copy to your bat file. Use your mouse to highlight it and CTRL-C and CTRL-V to copy it to the bat file you are working on. It's that simple !!! The other useful character is when you attempt to print something using a BAT file. Bat files are inherently STUPID !!!!!!! Once you print the file the paper stops feeding no matter where it is. You correct this by adding a form feed character to the Bat file using the COPY CON exactly as you did for the SOUND.BAT file. The special character for the form feed is CTRL-L Now using these special characters in your bat files requires the use of the ECHO command. Here is an example: REM REM This line beeps at ya echo ^G REM This line redirects a sheet of paper to the printer echo ^L > prn REM REMEMBER THAT the characters you see in your BAT file will not REM be ^G or ^L !!! They will be garbage characters (ASCII characters) REM I only displayed the ^G and ^L so you have an idea what I am talking REM about. Now if I only knew what I was talking about ! ------------------ Lesson 4: CALLING PROGRAMS AND MORE ON VARIABLES !!! Alrighty, remember lesson 2? That's the one that introduced you to variables in BAT FILES !!! Lesson 4 introduces you to another type of variable used by BAT files called a REPLACEABLE PARAMETER. In lesson 2 we entered choices from a menu we created in the BAT file. REPLACEABLE PARAMETERS are entered from the command line of the program. The entire purpose of this exercise is to demonstrate how to use a replaceable parameter in a BAT file and for this exercise we will create two bat files. One is absolutely useless. One is decent if you still use floppy disks. It's great if you can modify it for something other than a floppy disk !!! KEYWORDS: % - DOS Variable %1 - 1st variable entered on the command line %2 - 2nd variable entered on the command line %3 - 3rd variable entered on the command line and so on .... == Comparison of two strings NOTE !!! DOS was written using the C language. The comparison symbol in C is ==. That is why the comparison symbol in DOS is also == ! I just wanted to emphasize that that was no typo !!! REVIEW LESSON 2 for these keywords: GOTO : Shall we write a completely useless bat file just for kicks and giggles? *************** @echo off REM Remember why we use the @Echo off statement? Hint Lesson 1 REM BATCH FILE TO DISPLAY 9 REPLACEABLE PARAMETERS YOU ENTERED REM OH YA, WHY NOT add that sound you created in the Lesson 3 to the file REM to this file as well just to be annoying ! REM Display those 9 replaceable parameters as a sentence echo %1 %2 %3 %4 %5 %6 %7 %8 %9 REM Beep at me ! echo  REM Display those 9 replaceable parameters in reverse echo %9 %8 %7 %6 %5 %4 %3 %2 %1 REM Beep at me ! echo  end ************** Ok, now save this program as replace.bat and at a DOS prompt type the following: replace I am a real fool for following these directions! The program will then do the following: Display the sentence I am a real fool for following these directions Beep Display the sentence directions these following for fool real a am I Beep Get the idea? replace is the name of the program (replace.bat) I = %1 am = %2 a = %3 real = %4 and so on Remember we reversed the process so the sentence would display in reverse. Now of course just entering REPLACEABLE PARAMETERS at the command line serve no actual useful purpose if we don't check for them in our program somehow. That's where the the == statement comes in. There is only one efficient way to do this and that is with the IF statement. You may remember from LESSON 2 the ERRORLEVEL statement, however that is used with the CHOICE statement. Both the IF and ERRORLEVEL keywords will accomplish the same thing, however they do so in different manners. IF %1==password GOTO OK (used with REPLACEABLE PARAMETERS) ON ERRORLEVEL 1 GOTO OK (used with CHOICE keyword and other areas) Now lets write a small password program once. Essentially this is another useless program since a BAT file can easily be hacked to find out a password. It's not recommended that you store any passwords in a BAT file. This program is simply for demonstration purposes. ********* @echo off REM Let's write that password checker REM This line checks your password. If the password matches what you entered REM on the command line, the program jumps to the :ENTER statement. if %1=="mypassword" goto enter REM If the password does not match, the program jumps to the :END statement goto end REM The program comes here if your password matches. I'll explain how that REM startprogram.exe line works a little later on. :enter startprogram.exe REM The program comes here if your password doesn't match and then ends. REM Remember, the :END statement does not end the program !!! The BAT REM file ends when it has no more lines to execute. :end REM the BAT file ends after the CLS line. It doesn't end with the :END REM statement. CLS *********** OHHHHH!!!!!!!!!!! Remember I said I would explain that STARTPROGRAM.EXE a little later? Well now is a little later. Excited yet? In a BATCH file all you need to do to call another program in type the name of the program in the BATCH file ! It's that easy !!! Here's a practical example we all can use: ****** REM *** The start of a track installer REM Call a program called UNDAT to unpack a track UNDAT daytona.dat /a dat.txt REM Call a program called filez with a -o (overwrite) switch filez -o REM Call a program called DAT to pack the track DAT newdayt.dat /d dat.txt ***** NOOOOOOOOOO, I don't have this. I'm just using this as an example to show how to CALL OTHER PROGRAMS ! DOS is an extremely logical animal. This goes for Windows as well. Even if you do not place the name of program extension in the file they will know what you are talking about (generally). Why? Windows is a DOS based operating system. Yes, you heard me right. Windows 98 and 95 are both based on DOS 7.0. Without Dos 7 the Windows operating system wouldn't work worth a darn. DOS has something called a command parser (command.com) that is really nothing more than a file that contains basic commands that runs the operating system. As part of the basic operating system, DOS has a method by which it looks for files. If you want to run a program and don't type the extension, DOS will first look for a .COM program, then a .EXE program then a .BAT program. In that order. If it fails to find any of the above it reports a bad command or filename error (or your bat file does). Now, writing a BAT file is really getting a good grip on simple DOS commands and understanding how some of the basics of the DOS operating system works. So don't get bored that I'm off rambling about things like command parsers and other boring topics. In the long run you'll be glad. Okie, back to the calling of other programs. It's a pretty simple process really to call other programs. You can call just about anything you want from a BAT file as long as the files are executeable. You can even unzip files from bat files using pkunzip. Here's a neat little bat file I wrote to help me unzip a ton of files at a single time. pkunzip *.zip That's it !!! The entire bat file. Any questions? :) One other neat bat file I created to work with Marcello Bassino's DAT/UNDAT utility while creating tracks. I call it datfiles.bat REM Line 1 strips everything but the filename.ext from the file REM Line 2 shows the file size and tells me how many files in directory dir/b > datinfo.txt dir > datmost.txt Now if you haven't guessed it yet, everytime you use a basic DOS command like DIR in a BAT file, you are calling another program to the BAT file. BAT files are really quite useless by themselves. Any power they have is derived by what we put into them. Well that's it for lessone 4. In lesson 5 we are going to tie all of this together and actually create a track installer. Then in a final lesson 6 we will look at the keywords presented again (appendix of sorts) as well as some examples of different installers. Time once again for a break !!!!