Intermediate .BATch Tutor -by Barry Simon (c) Copyright 1986 by Capital PC User Group, Bethesda, MD. [Editors note: This article was published November, 1986 in the "Monitor" published by the Capital PC User Group. Material may be reproduced only for internal use by other not-for-profit groups.] One of the tools that most distinguishes the experienced DOS user from others is the effective use of BATch files. In this article, I will discuss some of the more advanced features of BATch files and their commands. I call this tutorial "intermediate" because I have tried to write in a manner accessible to those with only a little experience writing BATch files. ------------[ Simple BATch files ]------------ To begin with, a BATch file is a set of DOS commands which you place together and which you can issue by just typing the name of the BATch file. The classic example is the BATch file which changes to the directory containing Lotus' 1-2-3 and then runs 1-2-3. The same idea can be carried much further: for example, when I execute my word processor with a batch file, it -- loads the proper set of macros in my keyboard macro program; -- makes a mark using Kokkennen's MARK/RELEASE package; -- loads my thesaurus; -- loads the appropriate mouse menu program; -- runs the word processor, and -- after I am finished with the word processor, clears the macros and runs RELEASE to boot the thesaurus and mouse menu from memory. BATch files must be pure ASCII text files with separate commands on distinct lines separated by carriage return/line feed pairs. To create your BATch files, you can use EDLIN, a text editor, or any word processor that produces straight ASCII text files. BATch programs can contain DOS commands, application program commands, or a variety of specialized programming features that are known as BATch commands. ------------[ BATch Commands and Features ]------------ Parameters...... Your text editor may allow you to specify a text file to use as a parameter on the command line loading it. You would like to specify the file name as a parameter on the command line calling a BATch file to start your editor and have this file name passed on to the editor when it is loaded. This is quite easy. If your editor is called EDITOR.EXE and you load it via a BATch file called foo.bat, you need only make the line calling the editor say: editor %1 and then call up the BATch file as foo filename When the BATch processor comes to the %1, it looks for the first parameter following "foo" and replaces %1 by that parameter, in this case by filename. To be more precise, DOS parses the command line calling the BATch file into distinct "words" separated by one of four delimiters: , ; = That is, it looks for strings of allowed characters separated by one or more of these special four. Any of the 256 ASCII characters are allowed in the strings except for these four delimiters, the three redirection characters (<,>,|) and the ASCII nul. The first word on the command line is assigned the value %0, the next %l, etc. Normally, %0 is just the name of the BATch file but since "." is not a delimiter in parsing the line but is a delimiter in ending file names, if you have a BATch file foo.bat and type in foo,cpcugisgreat foo.bat will be run and %0 will have the full 17 character string assigned to it. Similarly, since DOS knows that file names are no more than 8 characters, the BATch file 12345678.bat will be run if you type in 12345678ABCDEFGHIJ but %0 will have an 18 character string. These are admittedly curiosities but DOS curiosities have a knack of being useful sometimes. In a real sense, DOS assigns as many parameters as there are words on the command line, but initially you can only access the first ten as %0 through %9. Any place that %1 appears in the BATch file except as %%1, it will be replaced by the first word after the filename even if that word is the empty word. Any other time that the symbol % appears in the BATch file, DOS will strip the %-sign away except that %% is replaced by a single percent and %%1 becomes %1 so if foo.bat has the single line: echo %%1 has the value %1 then typing foo junk will issue the message %1 has the value junk There is no limit on the size of any individual parameter other than the overall limitation that the entire DOS command line can contain no more than a total of 128 characters. To summarize: any time %i (for i=0,1,...,9) occurs in the file except as a %%i, it will be replaced by the ith string in the command line. The real limitation of BATch file parameters is that they are not variables. You cannot manipulate them by parsing them and cannot change their values. Labels...... As a preparation for discussing BATch file GOTO commands, I need to discuss labels. Any line beginning with the character ":" will be skipped by the BATch processor when it gets to it. This means that you can use such lines as remark lines which will not appear on the screen even if echo is on (in which case lines beginning with rem do appear). You can also place the redirection characters <,>,| on such a line (but you cannot place such characters on a "rem" line). And % signs are not treated specially on such lines. While DOS ignores such lines, the string following the : becomes a label for the GOTO command which I'll discuss next. The first word or the first eight characters of the first word become the name of that label. You can also place comments after a label name if you separate these comments with a space. GOTO...... The most significant way in which BATch files go beyond the DOS command line concerns two logical control structures, the GOTO and the IF commands. The line goto