home *** CD-ROM | disk | FTP | other *** search
- ~Basic Batch File Tips
-
-
- Using '%' Arguments in Batch Files - By Simon Burrows
-
-
-
- `If you've ever had the job of writing any but the simplest of
- `batch files before, you will have used arguments. Incorporating
- `these into such a batch file allows the user of the file to
- `specify one or more commands as well as the name of the .BAT file
- `at the Command Line. If you have never heard of arguments
- `before, or do not know how to use them, read on. This documents
- `contains a brief(ish) description of them and their uses :-
-
-
-
- ~`─────────────────────────────────────────────────────────────────
-
-
-
- `When using many of the utilities DOS comes with (eg, FORMAT.COM)
- `you must specify more than just the name of the file at the
- `command line. For example, when using FORMAT.COM, you may type
- `this to format a disk in drive A: to 3.5" HD :-
-
-
- ~FORMAT A: /F:1.44
-
-
- `This is exactly the same for many batch files.
-
- `For example, many older games available must be installed to a
- `hard disk with the use of a Batch File installation program. You
- `may need to type something like this at the command line to
- `install a game on a disk in drive A: to a directory called
- `SOLITARE on drive C: :-
-
-
- ~INSTALL A: C:\SOLITARE
-
-
- `When we do this, each part of the command is called an argument.
- `Whenever there is a space (eg, in between A: and C:\SOLITARE) a
- `new argument begins. These arguments can also be called
- `variables.
-
- `Each argument, or variable, is remembered by your computer as a
- `'%' sign and a number. So, the separate parts of the example
- `above are named as follows :-
-
-
- ~`INSTALL A: C:\SOLITARE
- ` │ │ │
- ` │ │ │
- ~` %0 %1 %2
-
-
- `The part called '%1' is the first argument even though it is
- `really the second, after '%0'. Most of the time you will never
- `use '%0', although, as you will see later in this text, it has
- `some uses.
-
- `If you are just using batch files, you needn't worry about these
- `'%' sign 'abbreviations' but they are an important part of the
- `writing of a batch file.
-
- `As an example, imagine you had a batch file which copies a file
- `from one disk to another. At the command line you specify where
- `the file is located at the moment and then its destination. For
- `example, you might type the following at the command line :-
-
-
- ~COPYIT A: C:
-
-
- `The batch file, COPYIT.BAT, would use DOS's Copy command to copy
- `the program. However, if you listed the file, it wouldn't just
- `read something like "COPY FILE.EXE A: C:" because this will
- `only copy the file from one set disk drive (A:) to another set
- `disk drive (C:). Instead, it would read something like this :-
-
-
- ~COPY FILE.EXE %1 %2
-
-
- `If you loaded this batch file now, stating the two arguments at
- `the command line to specify where the file for copying is now,
- `and where you want it to go, (eg, COPYIT A: C:), your computer
- `will replace the '%1' in the batch file with the first argument,
- `and the '%2' with the second argument. This means that you could
- `use this same batch file to copy the file "FILE.EXE" regardless
- `which drive it is on and which drive you want it to be copied to.
- `For example, if you typed :-
-
-
- ~COPYIT F: Y:
-
-
- `...your computer would automatically replace the %1 in
- `COPYIT.BAT's listing with 'F:' and the %2 with 'Y:' so it would
- `copy the file called 'FILE.EXE' from a disk in drive F: to a disk
- `in drive Y: (Just as long as you had these drives available on
- `your PC, of course!)
-
- `You could incorporate the use of variables and the IF command to
- `display a help message to the user of the batch file. For
- `example, in our example of "COPYIT.BAT", you could make it so
- `that if your user specified the first variable as '/?' (by
- `typing "COPYIT /?") a message would be displayed describing what
- `the file "COPYIT.BAT" does.
-
- `To do this you might use the following batch file listing :-
-
-
- ~`IF "%1"=="/?" GOTO HELP
- ~`COPY FILE.EXE %1 %2
- ~`GOTO END
-
- ~`:HELP
- ~`ECHO The file "COPYIT.BAT" will copy the file called "FILE.BAT"
- ~`ECHO from any drive to a destination of your choice.
- ~`ECHO.
- ~`ECHO Usage is as follows :-ECHO.
- ~`ECHO COPYIT [Location of "FILE.EXE] [Destination of "FILE.EXE]
-
- ~`:END
-
-
- `The top line of this is saying that if the user specifies the
- `first argument as '/?' (ie, by typing "COPYIT /?") then your
- `computer should go straight to the section of the batch file
- `called "HELP". This is the part directly after the ":HELP" line,
- `and has a series of 'ECHO' lines to display a text message.
-
- `However, if the user does not specify the first argument as '/?'
- `and instead types something like "COPYIT A: C:", then %1 will not
- `be '/?' so this first line will be ignored and it will go to the
- `next line. The next line, as described above, copies the file,
- `then, the line after that tells your computer to skip to the
- `section called "END" at the end of the file so it misses out the
- `unwanted help message.
-
- `So, if your user types "COPYIT X: Y:" the file "FILE.EXE" is
- `copied from disk X: to disk Y:, and if they type "COPYIT /?" the
- `following text-message is displayed :-
-
-
- ~`The file "COPYIT.BAT" will copy the file called "FILE.BAT"
- ~`from any drive to a destination of your choice.
-
- ~`Usage is as follows :-
-
- ~`COPYIT [Location of "FILE.EXE"] [Destination of "FILE.EXE"]
-
-
- `But what happens if your user doesn't specify any argument, and
- `instead loads the batch file by typing simply "COPYIT"? Well, if
- `this is the case then your computer will not know where the file
- `is and where to copy it to. In this eventuality you will need a
- `message displayed which tells the user that they have loaded the
- `file wrongly. This message might as well be the same as the one
- `displayed if they type "COPYIT /?" as this message (if your is
- `the same as mine, anyway) includes information on how to load
- `the file properly.
-
- `However, there's a problem. The ONLY way this message can be
- `displayed is if the file COPYIT.BAT is loaded with the '/?'
- `argument. Because of this, the only way to get the message to be
- `shown if no argument is specified is to make your computer reload
- `the batch file with the '/?' argument automatically.
-
- `To do this we need use of the '%0' argument mentioned at the
- `start of this document. Since '%1' stands for the first argument
- `after the filename, '%0' stands for the filename itself. This
- `means that you could put %0 into your batch file and the file
- `will reload when this figure is reached.
-
- `So, what we can do is say that if there is no '%1' specified,
- `then the batch file should be reloaded by the computer, this time
- `with the argument '/?', so that the message is displayed showing
- `the user how to use the batch file properly.
-
- By adding this feature to our batch file, it now reads :-
-
-
- ~`IF "%1"=="" %0 /?
- ~`IF "%1"=="/?" GOTO HELP
- ~`COPY FILE.EXE %1 %2
- ~`GOTO END
-
- ~`:HELP
- ~`ECHO The file "COPYIT.BAT" will copy the file called "FILE.BAT"
- ~`ECHO from any drive to a destination of your choice.
- ~`ECHO.
- ~`ECHO Usage is as follows :-
- ~`ECHO.
- ~`ECHO COPYIT [Location of "FILE.EXE"] [Destination of "FILE.EXE"]
-
- ~`:END
-
-
- `The first line checks to see whether there are no arguments
- `specified. It does this by seeing whether '%1' equals nothing
- `("" means nothing) rather than whether it exists at all. If '%1'
- `does equal nothing, it carries out the rest of the line (the
- `"%0 /?" part) which tells your computer to reload the batch file
- `with '/?' as the one and only argument. Otherwise, it skips this
- `part and continues to line two.
-
-
- So with this batch file, there could be 3 outcomes :-
-
- `1. The file is loaded with the first argument as the curent drive
- ` of the file for copying, and the second as the file's
- ` destination. In this case, the file will be copied correctly.
- `2. The file is loaded with the first and only argument being
- ` '/?'. In this case, the text message is displayed quickly
- ` explaining the file and how to use it properly.
- `3. The file is loaded with no arguments. In this case, the same
- ` text message is displayed quickly explaining the file and how
- ` to use it properly.
-
-
-
- ~`Well, there you go then folks, have fun!..
-
-
-
- ` ───────────────
- ~` Simon Burrows
- ` ───────────────
-
-
-