Last month we started to talk about Batch File Parameters. Let's go back over what the word "parameter" means. Parameters For our purposes here, a parameter is some additional information which you can include in the DOS command. Take the COPY command as an example. You can add to it the name of the file to copy, the destination to which the copy must go, and any DOS switches you may want to use. Your command might look like this: COPY FORMAT.COM B: /V The actual command is COPY, all the rest are parameters, with each one controlling some aspect of the copying process. The parameters tell COPY to copy the file FORMAT.COM from the default disk to the disk in drive B: and to verify that the copy is correct. Batch File Parameters For batch files, parameters are used in a different way from commands. You can add up to nine different parameters to the name of the batch file and DOS will assign a variable name to each of them. Then you can use this variable name in the form of a variable marker numbered from 1 to 9 (parameter number 0 is the batch file itself). To tell DOS you're specifying a replaceable parameter you precede the marker number with a percent sign, for example %4 is marker number four. Understanding Variable Markers Let's explain this with an example. We'll assume you've made a batch file called DELETE.BAT which looks like this ECHO ON ERASE %1 ERASE %2 and so on down to ERASE %9 Now, when you invoke (means use) the file called DELETE, you follow the batch file name with the names of the files you want to delete. Your command would be something like DELETE YOURFILE.OLD WP.DOC JUNK.DOC This specifies three parameters which are file specifications. When the second line of the batch file DELETE is called for, DOS substitutes YOURFILE.OLD, the first parameter you typed at the DOS prompt for %1. Then in order the files WP.DOC and JUNK.DOC are substituted for %2 and %3. With ECHO ON you can see your commands, so the screen shows ERASE YOURFILE.OLD ERASE WP.DOC ERASE JUNK.DOC DOS then tries to carry out commands for the remaining six parameters, but finding nothing, reports Required Parameter Missing six times. Practical work section: Constructing a batch file using parameters Your practical work this month is to make a batch file using parameters. If possible make a file to automate something you do regularly, like file copying or formatting. If that's a problem to you, here's an example. Its purpose would be to store the files you want to keep, on the hard disk of one of your several computers. We'll suppose you transfer information between computers by using floppy disks. You delete the file from the floppy disk after you've copied it back to a hard disk. So, to transfer data from floppy to hard disk, you must first copy the file to the hard disk, then erase the file from the floppy. Now make a batch file to do this. There are two steps, so there will be two commands, each on a separate line. Let's create the file, using your text editor as discussed earlier, and call it C&E.BAT (for Copy and Erase). First, the copy part. Try the command COPY A:%1 C:%2 /V followed by the erase part ERASE A:%1 and save the file as C&E.BAT. To use this file, you must put in some filenames to allow DOS something to work on. These filenames become the parameters %1 and %2. So, at the DOS prompt type C&E file1 file2 where file1 is the file to be copied and later erased and file2 is the new name for the copied file (you don't have to do this, it changes the file's name as it's being copied). If you type C&E File1 at the prompt, you will see on the screen C:\COPY A: File1 C:/V 1 File(s) copied C:\ERASE A:File1 C:\ DOS didn't find the second parameter so it executed the batch file using the same file name. File1 is copied from floppy to hard disk and deleted after that. The %2 parameter isn't used and the filename isn't changed. Try your file and see if it works! Additional batch file subcommands The REM Command is used mostly to include extra information in the batch file, for example, who wrote it, when and why. You put REM at the beginning of a line in your file. DOS will ignore the REM and all of the rest of the line completely. The ECHO command does two things. ECHO ON or ECHO OFF turns on or off the display of lines from the batch file as DOS executes the commands in those lines. ECHO is also used to display messages. If ECHO hasn't been turned off, then DOS displays REM statements on screen as they are encountered. So you can use REM with ECHO OFF for comments you don't want to display and use ECHO for those messages you want to see. Putting ECHO and REM in your batch files will be a great help when you come to use them. Unless you use a file regularly, for sure you'll forget what it does and why you made it the way you did. Cover this by leaving remarks (REMs) in your files. They don't appear on screen if you use ECHO OFF and they do make your file self-documenting, which you and other users will (later) appreciate very much. Don't forget the CLS command, which is used to clear the screen. In batch files, this is most common as a separate command after ECHO OFF. But compare it with the @ECHO OFF command as discussed earlier. However, you still need CLS to clear the screen at the start of a batch file. The GOTO Command This is a branching command. If you're familiar with the BASIC programming language or one of its derivatives, you'll know that GOTO enables you to jump to another part of your batch file. When you give DOS a GOTO command, it starts at the beginning of the batch file and searches for a label (where to GOTO). When the label is found, DOS then jumps to the line in the batch file following the label and continues to execute commands starting from that point. This way, with the use of the PAUSE command, you can create quite powerful batch files with loops activated by the PRESS ANY KEY WHEN READY... command. Batch file rules We won't pursue batch files beyond this. I'll conclude with a summary of he rules.
If you answer Y, the rest of the batch file is ignored and returns you to the DOS prompt. If you answer N, the current command in the file is skipped and DOS continues with the remaining commands to the end of the file. Batch files can do a lot of hard work for you. One command can replace a lot of repetitive typing but you must remember these key points
I'll be back next month . . . [ Ed. Unfortunately this was the last of the series. Sadly Ron Wilby
passed away a few months after this article was written. ]
|