Batch Copying Improvement (PC Magazine Vol 3 No 25 Dec 25, 1984 User-to-User) COPYNEW.BAT copies files selectively from one drive to another if the files to be copied are not on the targeted drive. COPYNEW verifies that the source and target drive designators have been entered and that they are not the same. To copy all of the files from the disk in drive A: that are not already on drive C:, you would type (at the DOS A> prompt): COPYNEW A C and hit the Enter key. You omit the colons after the drive letters; the batch file puts them in for you. echo off rem This program copies files selectively from one drive to rem another only if the file is NOT on the target drive. rem The syntax is: rem copynew x y rem where x and y are the source and target drives. Note rem these are entered like x and not like x: rem if x%1 == x goto error 1 if x%2 == x goto error 2 if %1 == %2 goto error 3 echo on %1: for %%f in (*.*) do if not exist %2:%%f copy %1:%%f %2:%%f goto end :error1 echo Source drive name missing goto end :error2 echo Target drive name missing goto end :error3 echo Source and Target drive names can't be the same goto end :end echo on ----------------------------------------------------------------- Batch Test for Empty DOS Variable (PC World Nov 1984 The Help Screen) An on-line help system consisting of several ASCII files with a full screen of information on a particu-lar command, can be developed. A batch file called HELP.BAT calls up the screens. The ASCII files are straightforward. Each should be given the name of the corresponding command and the extension .HLP; for example, COPY.HLP gives information on the COPY command. HELP.BAT is also straightforward. Its only instruction (other than coloring the screen to identify a "help" screen) is TYPE %1.HLP. A HELP file can also be included which explains how to use the help facility. Including the HELP file requires that HELP.BAT be prepared as: if exist %1.hlp goto does if x==%1x goto help echo Help unavailable for %1 goto end :does type %1.hlp goto end :help type help.hlp :end ----------------------------------------------------------------- Subdirectory Fixer II (PC Magazine Vol 4 No 3 Feb 5, 1985 User-to-User) User-to-User published a trick using TREE.COM, TREEFIX.BAT, and TREEFIX.BAS to locate files across subdirectories. While the TREEFIX trio works -- and provides information that DOS does not -- the FINDFILE.BAT batch file below provides an easier way to find a file buried deep inside one of your subdirectories. FINDFILE relies on a little-used feature of CHKDSK.COM, the /V option. The DOS manual is a bit murky on CHKDSK and most users don't do much more with this command than see how much space is left on their disks. The /V option lists all files in all subdirectories. The DOS 3.0 manual is more helpful both in explaining the many features of the powerful CHKDSK.COM command. FINDFILE.BAT first makes sure you have CHKDSK.COM, FIND.EXE, and MORE.COM on your disk. If you're PATHing to a subdirectory that contains these files, you should leave out the dozen lines beginning with the second one ("if exist chkdsk.com ...") and ending with ":C". FINDFILE then redirects CHKDSK/V into a file and uses the FIND filter (and the MORE filter if appropriate) to locate the file in question. To search for BASICA.COM, for instance, you would type: FINDFILE BASICA. If you typed: FINDFILE BASIC the batch file would locate both BASIC.COM and BASICA.COM, and any other filename with the capital letters BASIC in it. You may also use parts of names. FINDFILE BASICA would find BASICA.COM. This comes in handy if you want to look for files with the same extensions. FINDFILE .COM will list all your .COM files. Remember to enter capital letters only. And don't put quotation marks around the filenames or parts of filenames you want to find -- the batch file will do this for you automatically. FINDFILE won't display a special message telling you no matches were found if it comes up empty. But this will be obvious when no matches are displayed on your screen. FINDFILE.BAT batch file to locate specific files across all subdirectories on your disk: echo off if exist chkdsk.com goto A echo PUT CHKDSK.COM on your disk goto D :A if exist find.exe goto B echo PUT FIND.EXE on your disk goto D :B if exist more.com goto C echo PUT MORE.COM on your disk goto D :C echo NOW SEARCHING DIRECTORIES FOR "%1" chkdsk /v>-------- find "%1" -------- | more del -------- :d rem ...DONE ----------------------------------------------------------------- Better FIND (PC Magazine Vol 4 No 7 April 2, 1985 User-to-User) The FINDFILE.BAT (see article above) is a useful utility. To make the heart of the program, subroutine :C, more elegant, use DOS's piping rather than redirection. The 15th line of the replacement FINDFILE.BAT below replaces the second, third, and fourth lines in subroutine :C of the original FINDFILE.BAT. With piping, DOS automatically takes care of creating and deleting the intermediate files. The new, improved FINDFILE.BAT: echo off if exist chkdsk.com goto A echo PUT CHKDSK.COM on your disk goto END :A if exist find.exe goto B echo PUT FIND.EXE on your disk goto END :B if exist more.com goto C echo PUT MORE.COM on your disk goto END :C echo NOW SEARCHING DIRECTORIES FOR "%1" chkdsk /v | find "%1" | more :END REM ...done ----------------------------------------------------------------- DOS FIND Sieves (PC Magazine Vol 4 No 8 April 16, 1985 User-to-User) The DOS FIND filter gives users a fast way to retrieve information from large databases, document files, programs or any ASCII file. But there's a way to make it even more useful. This can be done with the SEARCHUM.BAT file below. If you want to retrieve from a large client address file called CLIENT.DAT all cases in which the client is female, lives in Boston and is self-employed, the command is: SEARCHUM CLIENT.DAT "female" "Boston" "self-employed" SEARCHUM.BAT first turns off the echoing of subsequent command lines to the screen. The first FIND command searches file CLIENT.DAT (passed as dummy variable %1) for any instance of the first characteristic, "female", passed via dummy variable %2. All such lines are written to the first of the SIEVE#.DAT file via redirection of output along with the sequence line number since the /N parameter was used with FIND. Then each subsequent command line searches for the subset of lines in the latest SIEVE#.DAT file that contains the character string passed to the command line via the dummy variables. If no lines contain all the properties that you specify in the list, then one or more of the later SIEVE#.DAT files will be empty. Just search back until you find the file with the information you seek. DOS 2.0 allows up to nine dummy variables in the list after the command file name. So this method lets you specify up to eight characteristics in addition to the name of the file you wish to have searched if you extend the number of FIND command statements within the .BAT file. Instead of requiring that all the properties in the list be present simultaneously, you can structure the .BAT file so that you can find lines that contain any (i.e., logical OR) of a list of characteristics and then concatenate the accumulated files at the end with a COPY command. Or, you can tailor a search to detect some combination of jointly present and alternative conditions. On a PC-XT, FIND can readily scan and report the presence of two instances of a unique character sequence located in the middle and at the end of a 7,000-line file in about 10 seconds. For ASCII files under a few thousand lines, the response seems instantaneous. SEARCHUM.BAT: ECHO OFF FIND/N %2 %1 > SIEVE1.DAT FIND/N %3 SIEVE1.DAT > SIEVE2.DAT FIND/N %4 SIEVE2.DAT > SIEVE3.DAT Editor's Note: It is possible to save a bit of keystroking by putting the quote marks in the batch file (e.g., %1" rather than %1, "%2" rather than %2, etc.) so you don't have to type in the quote marks when beginning the search, but this works only if you're searching for single words. And FIND is case-sensitive, so you have to be sure your database doesn't contain the words female, Female and FEMALE. ----------------------------------------------------------------- Clear-Cut TREEs (PC Magazine Vol 4 No 8 April 16, 1985 User-to-User) The DOS TREE utility shows the structure of all subdirectories ies on a disk, but its display is not very clear. The TREEFIX.BAT file redirects the DOS TREE output to a file, TREE1.LST, which is then processed by the BASIC TREEFIX.BAS program to give you a much clearer picture of how your subdirectories are arranged. To use this, you can either have TREEFIX.BAT and TREEFIX.BAS on the disk you want to examine and just type TREEFIX while in DOS, or you can specify a separate drive to examine, for instance, TREEFIX B:. (Note: TREEFIX.BAS is on the BASIC disk. TREEFIX.BAT: ECHO OFF ECHO Now searching directory ... TREE %1>TREE1.LST BASICA TREEFIX.BAS Editor's Note: Be sure to either have TREE.COM and BASICA.COM on whatever subdirectory the two TREEFIX files are on, or have a PATH to TREE.COM and BASICA.COM. This technique works with up to six levels of subdirectories; after that the display will wrap. ----------------------------------------------------------------- FOR Play (PC Magazine Vol 3 No 24 Dec 11, 1984 User-to-User) FOR allows you to execute a DOS command for each of the files in a specified set of files. It's real usefulness comes from the fact that the set of files can be specified using wildcard names. But FOR works only from within batch files. To perform the same operation with a set of files with the same extension, create a batch file called REPEAT.BAT that contains one line: FOR %%A IN (%2) DO %1 %%A %3 The syntax for running the batch file is: REPEAT program filespec [/program switches] The first command line parameter becomes the name of the program to be run, the second parameter is the set of files to be worked on, and the third parameter is any switches that program may need after the program name. For example: REPEAT EDIT *.DOC would translate into the FOR command required to edit a set of DOC files. REPEAT is also useful for the many public-domain utilities that do not allow wildcard filenames. ----------------------------------------------------------------- Nested Batch Files (PC Magazine Vol 4 No 12 June 11, 1985 User-to-User) You can nest batch files simply by putting COMMAND/C before the name of the batch file to be called. When each file completes execution, control returns to the calling batch file. This feature is sort of documented in the DOS manual under "Invoking a Secondary Command Processor," but the manual doesn't really tell how to use it this way. Tests indicate that performance doesn't degrade when in the secondary processor. To see how this works, create the four batch files LEVEL1.BAT, LEVEL2.BAT, LEVEL3.BAT, and LEVEL4.BAT. To run it, type LEVEL1. Once all the batch files have finished running, you'll see a list of echoed "Return to ..." messages onscreen that shows how the files were nested. In an unrelated subject, to print a list of subdirectories, something DOS does readily, just type: DIR|FIND"" Four simple files to demonstrate batch file nesting using COMMAND /C to pass control. To use this properly, create the BEEP.BAS program and have it and BASICA.COM on disk: echo off echo THIS IS LEVEL1.BAT basica beep command /c level2 echo Returned to LEVEL1.BAT echo off echo THIS IS LEVEL2.BAT basica beep command /c level3 echo Returned to LEVEL2.BAT echo off echo THIS IS LEVEL3.BAT basica beep command /c level4 echo Returned to LEVEL3.BAT echo off echo THIS IS LEVEL4.BAT basica beep 10 BEEP 20 FOR DELAY=1 TO 100:NEXT 30 SYSTEM -------------------- Two ways to list subdirectories. Make sure proper combinations of TREE.COM, FIND.EXE, CHKDSK.COM, MORE.COM and SORT.EXE are on your disk or PATHed to: tree /f | find "Path" | more chkdsk /v | find "Directory" | sort | more ----------------------------------------------------------------- The Fully Powered PC (PC World July 1985 by B. Alperson, et. al.) The purpose of batch files is to automate commonly used procedures. But often you may want to modify the way a procedure works at the time you call its batch file. For this DOS provides replaceable parameters. When you create a batch file that will have variable information, simply use a replaceable parameter variable at each point where such information will be needed. Replaceable parameter variables consist of a single percent sign and an integer (%0 through %9). When you execute the batch file, any words you type on the command line following the batch file name will be substituted for those variables. DOS assigns the value of each replaceable parameter in sequence, according to the position of the words on the command lines. The name of the batch file, the first word on the command line, is used as the value for parameter %0. The next word on the command line is the value for replaceable parameter %1, etc. Consider the word processing batch file GO.BAT (below). It includes the replaceable parameter %1 in two lines: the line that calls a directory and the line that calls WordStar. If you were to invoke this file by typing GO DOCUMENT.DOC, the parameter DOCUMENT.DOC would be substituted for each %1. Thus, the line `DIR B:%1/W' would be interpreted by DOS as DIR B:DOCUMENT.DOC/W, and DOS would search for the file DOCUMENT.DOC. The line `A:WS %1' would be interpreted by DOS as A:WS DOCUMENT.DOC. By WordStar convention, the command WS filename loads WordStar and then opens the specified file. WordStar would then bypass its opening menu and go immediately into document mode with the file DOCUMENT.DOC in place for editing. Before DOS 2.0, you were restricted to ten replaceable parameters per batch file. With the SHIFT command DOS 2.0 removed the ten-parameter restriction. Now the only limitation on the number of parameters is that they all fit on a logical command line. (A logical command line is everything you type from the DOS prompt through the carriage return and may be up to 127 characters.) Imagine a command line with 14 elements: A>command w1 w2 w3 w4 w5 w6 w7 w8 w9 w10 w11 w12 w13 You still have only ten replaceable parameter variables available, %0 through %9. The first ten words on the command line (the command itself and w1 through w9) are assigned to those variables. The SHIFT command does not increase the number of variables; but each time you use it, the variable assignments are "bumped over" one position. With the first SHIFT command, the variables %0 through %9 are reassigned the values w1 through w10, respectively. The value originally assigned to %0 is bumped off. Use the SHIFT command again, and the variables %0 through %9 acquire the values w2 through w11. However, the true value of the SHIFT command goes beyond getting more replaceable parameter values from the command line. The following application shows how useful SHIFT can be. DOS 2.0 provides a PRINT command to set up a print queue but the command has at least two serious problems. The queue created by PRINT is limited to 10 files, and the PRINT command does not recognize path names. Consequently, all the files to be printed must be in the same directory, and you have to print from that directory. (DOS 3.0's PRINT command does not have those limitations. The optional /Q: command enables you to specify the number of files -- up to 32 -- that the queue will hold. The PRINT command in DOS 3.0 also recognizes path names.) QUE.BAT shows how the SHIFT command can set up a queue program in DOS 2.0 (in this case called QUE.BAT) without the limitations imposed by the PRINT command. QUE.BAT is invoked with the following syntax: QUE filename1, filename2, filename3 ..... This batch file allows you to queue up to 62 files, depending on the length of the file names. The printout of each file begins on a new page with a header consisting of the file name and a line of hyphens. The first two lines of QUE.BAT turn off the echo and clear the screen. The fourth line, `A:BASICA A:FORMFEED', makes use of a simple BASIC program called FORMFEED.BAS to start the file on a new page. FORMFEED.BAS consists of the line: 10 LPRINT CHR$(12);:SYSTEM 'ECHO PRINTING FILE %1' displays the name of the file currently being printed. The next three lines send output to the printer: the first sends the current file name; the second sends a line of hyphens, which is part of the header; and the third TYPEs the file's contents. All output is redirected to the printer with `>PRN'. By using the TYPE command instead of PRINT, QUE.BAT allows you to use path names freely in file names specified as replaceable parameter values for QUE.BAT. The `:PRINT' and `IF NOT ZIP==ZIP%1 GOTO PRINT' lines in QUE.BAT are discussed below. These lines determine whether more files must be printed. If not, the batch file terminates. The SHIFT command makes this batch file work. The first time through the batch file, the value %1 is filename1; all references to %1 use that value. But before the procedure in the batch file repeats, the SHIFT command is invoked, causing all references to %1 to use the value filename2. The third time through the batch file, %1 takes on the value filename3, and so on. Thus, the SHIFT command allows the batch file to pick up each file name on the command line with the use of a single DOS variable. Normally the execution of a batch file flows from the first to the last statement in the file. With branching you can redirect the flow. Branches can be conditional or unconditional. An unconditional branch uses the syntax GOTO label. GOTO transfers control to the line containing the next executable command following label. The label can be any string of up to eight characters prefixed by a colon. The label line can include other words; however, the label must be the first word on the line. The conditional branch allows the batch file to choose between actions depending on whether a certain condition is met. The command uses the syntax: IF [NOT] condition command The condition parameter names any of three conditions for testing. It can test whether or not two string values are the same (IF [NOT] string1==string2 command). It can test for the existence of a file (IF [NOT] EXIST filespec command). Finally, condition can test for an exit code (IF [NOT] ERRORLEVEL number command). When a program terminates, it can set a numerical value, called an exit code that indicates the manner in which the program terminated. By checking this value with ERRORLEVEL, you can test whether the previous program terminated successfully or with an error. This test is potentially useful, but the only programs in DOS 2.0 that produce exit codes are BACKUP and RESTORE. Unless your application involves these two commands or sets its own exit codes, ERRORLEVEL is of limited value. The EXIST filename test is crippled by the exclusion of path names from the filename parameter. The specified file must be in the default directory or it will not be found. This limitation has been eliminated in DOS 3.0. However, even the weak IF commands of DOS 2.0 add substantially to your control over batch operations. The QUE.BAT batch file would not work if it weren't possible to branch within the batch file. The critical line is the conditional branch instruction `IF NOT ZIP==ZIP%1 GOTO PRINT'. Each time the batch file reaches this point, it tests whether the string `ZIP' is the same as the string `ZIP%1'. If the value of %1 is filename1, the line finds that `ZIP' does not equal `ZIPfilename1', control is transferred to the next executable line after the label `:PRINT', and the file is printed. The SHIFT command bumps the assignment of %1 to the next value, filename2. The test is then performed again, with the same outcome. Finally, no file names remain on the command line. At this point the test finds that `ZIP==ZIP%1', because %1 has no assigned value. Therefore, the GOTO command is not executed, and control passes to the next line. A final formfeed is sent to the printer, and the batch file terminates. `ZIP' is used as part of the string test because batch file conditional commands cannot compare a string to a null value. Some character(s) must be included, and the arbitrary choice ZIP is as good as any other. Also note that the replaceable variable is placed on the right side of the double equal signs. This prevents the errant behavior that occurs when the right string matches the beginning of the left string but is shorter. DOS 2.0 also allows you to generate batch file loops. Within certain limits, you can have the batch file recycle the same command with different arguments. To do this, use the syntax: FOR %%variable IN (set) DO command The DOS manual tells you, "The %% variable is sequentially set to each member of set, and then the command is evaluated and executed." You may find it more helpful to think of the command as follows: FOR each item IN (this collection of items) DO the action specified in the command using the items appropriately. For example, if you had to back up three files in a financial program and you wanted to copy those files from drive A: to drive B: with a batch file, you could use the command: FOR %%Z IN (TRNS.DOC CHKS.DAT DEPS.LST) DO COPY A:%%Z B: %%Z is a variable whose value is set sequentially to each name in the collection of items. The batch file would operate once as though instructed to COPY A:TRNS.DOC B:. It would then operate as though instructed to COPY A:CHKS.DAT B:. Finally, it would act as though instructed to COPY A:DEPS.LST B:. Since no items would remain in the set at this point, the loop would terminate. DIRALL.BAT shows how the FOR command can keep track of files within a complex directory structure that is possible in DOS 2.0. For example, DIRALL.BAT, with the command DIRALL *.BAT, checks each of the 14 subdirectories listed in the FOR commands, the root directories of drives A: and C:, and reports to the printer the existence of each batch file. These reports are arranged within each of the 16 subdirectories and the 2 root directories. You would have to type 18 DIR commands, 1 for each directory, to duplicate the behavior of the single command DIRALL. DIRALL shows that you can intermix DOS command line variables (replaceable parameters) and the item variable of the FOR command in batch files. The FOR command's item variables are identified with a double percent sign and an alphanumeric character (%%0 through %%9 and %%A through %%Z). The FOR command can also be used from the command line, in which case you must change the double percent signs preceding the letter variable to a single percent sign. For example, at the command line you would type FOR %Z IN (A:\B:\) DO DIR %Z to display the root directories of drives A: and B:. One of the most powerful techniques employed in programming is the subroutine, a program statement or set of statements that accomplishes a specific task. Batch files have always been able to call other batch files into execution. Normally, however, the second batch file does not return control to the first. Although the capability is not well documented in the DOS manual, in versions of DOS later than 2.0, a batch file itself can be a subroutine and can be invoked at any time by calling it with an instruction in another batch file. Once it has accomplished its task, the subroutine batch file automatically returns control to the next instruction in the calling batch file. You call for the batch file subroutine with the syntax: COMMAND/C filespec [%# ...%#] COMMAND/C is the subroutine call (equivalent to GOSUB). Filespec lists the drive, the path, and the name of the batch file to use as a subroutine; %# ...%# are the values for any replaceable parameters in the subroutine. The only special attribute of the subroutine batch file is that it must contain the word EXIT as its last command. This command is the equivalent of RETURN in many computer languages returns control to the parent batch file. You could, for example, set up an AUTOEXEC.BAT file to perform three tasks: setting up the clock, setting up the monitor, and setting up the printer. If each of these tasks were a subroutine, your AUTOEXEC.BAT file would consist of only three major instructions. You would set up the file as follows: ECHO OFF CLS COMMAND/C SETCLOCK ECHO OFF CLS COMMAND/C SETMON ECHO OFF CLS COMMAND/C SETPRINT The three files serving as batch subroutines would be: SETCLOCK.BAT: ECHO OFF CLS SETCLOCK EXIT SETMODE.BAT: ECHO OFF CLS MODE 40,R EXIT and, SETPRINT.BAT: ECHO OFF CLS MODE COM1:96,N,8,1,P MODE LPT1:=COM1 EXIT You can "nest" batch file subroutines; that is, one subroutine can call another, which can call another, etc. But be careful; each level of nesting will cost you 3K of RAM. Also remember that DOS always turns on the echo at the beginning of any batch file. To keep the echo off, begin each batch file with the command ECHO OFF. Unfortunately, this command, like the first command of all batch files, is displayed. But it disappears almost immediately if you follow it with a CLS command. The AUTOEXEC.BAT file shown earlier is too simple to be of much use, but it illustrates the techniques to use for more complex subroutine operations. By using batch file subroutines you can easily maintain and modify batch files of great complexity. This technique also reduces debugging problems dramatically by locating instructions in well-defined modules, and it enables you to write the instructions for a commonly used procedure only once and then call the routine from any batch file. ----------------------------------------------------------------- BASIC Switches and Concurrently OPEN Files (PC World July 1985 The Help Screen) You can use BASIC's switches and a filename with the DOS commands BASIC or BASICA to call a BASIC program from DOS and still use the BASIC switches /F (to set the maximum number of files that may be OPEN simultaneously) and /S (to set the file buffer size to match the maximum record length the BASIC program uses) by putting the BASIC program you're calling as the second parameter on the DOS command lines. For example, in this excerpt: . SORT /+15 < A:SUBCEN.DAT > B:BAIN.DAT BASICA BARPT SORT /+73 < B:BAOUT.DAT > B:CASE.DAT BASICA CASRPT /F:7/S:512 . You can also include stdout redirection when calling a BASIC program that switches, but the redirection parameters in a BASIC call must precede the switch parameters. You also need to include the line FILES=11 in your CONFIG.SYS file. The FILES=x parameter sets the number of simultaneously OPEN files by reserving enough RAM for x file handles. All file accesses -- reads, writes and closes -- can then be performed by telling DOS which file handle to use. But because DOS uses three file handles for stdin, stdout, stderr, stdaux and stdprn, and BASIC uses one more file handle for LOAD, SAVE, CHAIN, NAME and MERGE commands, the FILES= parameter in the CONFIG.SYS file must be set to exceed by four the number in BASIC's /F: switch. ----------------------------------------------------------------- Quick Text Editor (PC Magazine Vol 4 No 18 Sept 3, 1985 User-to-User) BUILD.BAT lets you create text files or short programs easily. When in DOS, enter "BUILD filename" where filename is the name of the file you wish to create. BUILD.BAT will clear the screen, display a ruler line and save all your input in an ASCII file with whatever filename you assigned. When you're finished entering text, hit the F6 key and Enter. If a file with the same name as your filename already exists, BUILD.BAT will rename it with a .BAK extension. By specifying PRN: as the filename, all text entered is dumped to the current list device. This is useful for short memos or notes. BUILD.BAT does not allow text to be edited except for the current line. But it's useful to create small batch files or write memos or address envelopes quickly and easily in DOS. And it's forgiving enough not to write over an existing file. BUILD.BAT: ECHO OFF IF %1==PRN: GOTO START IF NOT EXIST %1 GOTO START REN %1,????????.BAK :START CLS ECHO 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 ECHO ----!----!----!----!----!----!----!----!----!----!----!----!----!----!----!---- COPY CON:=%1 :END ----------------------------------------------------------------- Quick One-Liner (PC World September 1985 Star-Dot-Star) CPY.BAT copies files from the default disk drive to another drive, copying only those files that don't exist on the second drive: for %%F in (%1) do if not exist %2%%F copy %%F %2 CPY.BAT is invoked with the command, CPY filespec d:. For example, the command CPY *.DOC A: copies all files with the extension .DOC from the default drive to drive A:. Path names will not work with it unless the corresponding directories exist on both disks, and the source drive must be the default disk drive. ----------------------------------------------------------------- Batching Back to a Previous Directory (PC World October 1985 The Help Screen) Batch files are used extensively as menu selections for specific applications. For example, if Lotus 123 is in a separate subdirectory, a batch file can automatically switch to that subdirectory, prompt the user to place the system disk in drive A:, and load LOTUS.COM. When LOTUS.COM finishes, the batch file returns the user to the root directory and displays the main menu. Since not all users are DOS wizards, a series of help screens for the DOS commands can be used using the HELP.BAT routines (PC World May 1985 and PC World December 1984). However, if the help screens are kept in their own subdirectory, a problem arises. To execute the IF EXIST %1.HLP batch command, which tests for the existence of a specified help file, you must be in the directory where the help screens are located. To get HELP.BAT to work, you have to change to the HELP subdirectory. In creating the batch file, the alternatives are to leave users in the HELP subdirectory or return them to the root and the main menu. Editor's Note: DOS 3.0 and later versions accept paths in the IF EXIST command, so you don't have to leave the current directory to test whether a file exists in another directory. For DOS 2.x, output redirection and BASIC can get the user back to the directory from which HELP.BAT was called. Modify HELP.BAT as follows: echo off cls cd >c:\help\original.dir cd c:\help if exist c:%1.help goto does if x==%1x goto help echo Sorry. Help unavailable for %1 goto end :does type c:%1.hlp goto end :help type c:help.hlp :end pause basica c:reset_cd There are three basic differences between this HELP.BAT and the one used. The line "cd>c:\help\original.dir" redirects the output of this CD command (display current directory) into a file named ORIGINAL.DIR. The drive designator C: has been prefixed to the file names in HELP.BAT so that those files are found even when C: is not the default drive. And the new batch file ending, "basica c:reset_cd" returns the user to the directory that was current when HELP.BAT was called. Load BASIC(A) and create RESET_CD.BAS: 10 OPEN "C:ORIGINAL.DIR" FOR INPUT AS #1 20 INPUT #1,A$ 30 CHDIR A$ 40 SYSTEM Use the command SAVE "C:\HELP\RESET_CD" to save this program in the HELP subdirectory on drive C:. This program reads the name of the user's original subdirectory from the file ORIGINAL.DIR, changes to that directory and exits to DOS. This routine assumes that BASIC(A) and HELP.BAT are available via the extended directory search path defined by the latest PATH command. For example, with HELP.BAT in a directory called BATCH and BASIC(A) in a directory called DOS, your hard disk's AUTOEXEC.BAT file might contain the command "PATH=C:\BATCH;C:\DOS;" so that the commands in those directories are available from any directory. ----------------------------------------------------------------- Creating Batch Files With Echo (PC World November 1985 Star-Dot-Star) You can reduce the space that batch files use by having the ECHO command create them on a RAM disk. Since a single batch file can contain any number of ECHO commands, a couple of large batch files filled with ECHO commands can replace dozens of the space-wasting kind. An as example, you can include the line: echo basica menu.bas/ f:6 > d:menu.bat among others in an AUTOEXEC.BAT file. (You can place it in any batch file, as long as it executes before you need the file it creates.) The greater-than symbol sends the output of the ECHO command to a file instead of to the screen. When the AUTOEXEC.BAT file executes, it creates the batch file MENU.BAT on drive D:. The RAM disk runs the batch file more quickly than a floppy. This technique can be enhanced by using the FOR subcommand. For example, the command: echo for %%%%f in (cls dbase pause d:menu) do %%%%f > d:db.bat creates a batch file that clears the screen, runs dBASE, waits for you to press a key, and then runs a menu program on drive D:. Note that the lines use quadruple percent signs; they are necessary because DOS strips some of them as the command executes. Editor's Note: You can create multiline batch files by sending several ECHO statements in a row to the same file, using double greater-than signs to append the new lines to those already sent. For example, if you place the lines shown in BUILDBAT.BAT below in an AUTOEXEC.BAT or other batch file, they create a five-line batch file called D.BAT, which turns echo off, clears the screen, changes the DOS prompt, waits for the user to press a key, and then lists the directory of the current disk. Also, with the aid of the IF subcommand it's easy to create a complex menu system that uses just two batch files. - - - - - BUILDBAT.BAT: echo echo off > d.bat echo cls >> d.bat echo prompt $p$g >> d.bat echo pause >> d.bat echo dir >> d.bat ----------------------------------------------------------------- Batch Tricks (PC Magazine Vol 4 No 26 Dec 24, 1985 User-to-User) With DOS 2.1, it's simple to create blank lines in batch files by typing ECHO following it with two spaces. However, in DOS 3.1, this trick won't work. DOS will interpret an ECHO followed by blanks as a request for the current ECHO status, and since most users turn ECHO OFF in the first line of a batch file, all you'll get is an "ECHO is off" message. If you create your batch files using the DOS COPY CON: command, the way to get around this and end up with a blank line in DOS 3.1 is to type in the word ECHO and then hold down the space bar until the cursor wraps to the next line. When the cursor moves down to the next line, type your message. Another batch file problem is that it's handy when writing instructions for novices to refer to the Enter key by actually drawing on-screen the crooked arrow that IBM put on the Enter key. You can assemble a representation of this crooked arrow by printing characters 17, 196, 196, and 217. To see this, run this BASIC program: 10 PRINT CHR$(17); 20 PRINT STRING$(2,196); 30 PRINT CHR$(217) However, while DOS allows you to print virtually any of the 256 possible characters on the screen by holding down the Alt key, typing in the ASCII number of the character on the number pad, and then releasing the arrow key, it can't handle characters with ASCII values lower than 32. One way to create an arrow is to first load BASICA, type in a line number, a space, the "remakr" statement REM, and a space. Then use the Alt plus number pad trick with 17, 196, 196, and 217. Save the file as ARROW.BAS. Then load your word processor and delete the line number and the REM statement, leaving just the arrow itself. If you use WordStar, the arrow will appear as ^QDDY, but it will resemble an arrow once you're back in DOS. They you can insert this pictorial arrow into any batch file instead of having to say "Hit the key." Editor's Note: While the first technique does work, you have to add several extra spaces at the beginning of your message on the second line to compensate for the space taken up by the word "ECHO," or else this message will start as the end of the first line. Also, the DOS COPY CON: trick can handle only 127 characters, which limits the kinds of messages you can write. A better way to end up with a blank line in DOS 3.1 is to type ECHO, follow it with one or two spaces, and then follow the spaces immediately with a CHR$(255), which you can produce by holding down the Alt key, typing 255 on the number pad, and then releasing the Alt key. The ECHO+space+space+CHR$(255) blank line trick works in DOS 3.1. Who knows what Microsoft and IBM will do with future versions? There are two easier ways to display the "arrow" (Enter) key. One is to write a small BASIC file: 10 OPEN "arrow." FOR OUTPUT AS #1 20 PRINT #1,CHR$(17); 30 PRINT #1,STRING$(2,196); 40 PRINT #1,CHR$(217) 50 CLOSE Another even simpler way is to use DEBUG: A>debug -n arrow -e 100 11 c4 c4 d9 -r cx -4 -w -q ----------------------------------------------------------------- Disk Memo Pad (PC Magazine Vol 4 No 26 Dec 24, 1985 User-to-User) The DD.BAT file creates a small memo or diskette description file and lets you update it automatically without having to use a word processor. You can copy the small DD.BAT file to all your disks and use it to remind yourself what's on the disk -- and update the reminder automatically. After you've entered any information, typing DD will display it. To update the information, just type DD followed by up to nine words of text. The next time you type DD the new message will appear, appended to the old. Editor's Note: The nice thing about this is that you can leave memos to yourself up to 23 lines long and update the memos without having to load a word processor. The ^G in the 7th line from the bottom is a beep. You can enter this in WordStar by typing Ctrl-P Ctrl-G. DD.BAT: echo off if z==z%1 goto display echo %1 %2 %3 %4 %5 %6 %7 %8 %9 >> %0.doc :display cls if not exist dd.doc goto :oops type %0.doc goto end :oops echo ^GYou haven't entered anything yet ..... echo To enter date, type DD and then type up echo to 9 words on each line. echo You can enter up to 23 lines. echo ---- echo To see what you've typed, just type DD :end ----------------------------------------------------------------- ...To the Rescue (PC Magazine Vol 3 No 14 July 24, 1984 User-to-User) Most computers can provide a help facility. By typing the word HELP, optionally followed by a program name, the user gets a quick how-to on the requested program. Create the batch file HELP.BAT (below). Then, using your word processor (of simply by typing COPY CON::HLP, create files with the .HLP extension that explain each of the programs. With this batch file and your .HLP files, you can keep lots of information at your fingertips. To hold the files in a subdirectory called HLPFILES, use CD\HLPFILES at the DOS prompt. HELP.BAT: echo off cls if %1==SUBJECTS goto subjects if %1==subjects goto subjects if not exist \hlpfiles\%1.hlp goto nofile copy \hlpfiles\%1.hlp con goto endbatch :subjects dir \hlpfiles\*.hlp/w goto endbatch :nofile echo Type HELP followed by the file name (or HELP * for all) echo Type HELP subjects for a list of available topics :endbatch ----------------------------------------------------------------- Magic Batch Fingers (PC Magazine Vol 4 No 2 Jan 22, 1985 User-to-User) Some DOS commands, such as FORMAT and ERASE*.*, require keyboard input and thus cannot be used in an unattended batch file. For example, if you enter FORMAT A:/V, you will be instructed to place the disk in drive A: and to press any key to continue. You will then be asked to enter a VOLUME ID for the disk, and then whether you want to format another. If this FORMAT command were command executed from a batch file, the batch processing would stop and wait for your keyboard responses to these prompts. Here's a method to eliminate the keyboard input. First, either with a word processor or using the DOS COPY CON: command, construct a file called RESPONSE that contains the responses: A>COPY CON:RESPONSE NEWDISK N ^Z The first line copied to the file is a blank line produced by just pressing Return. This is important since it contains the Return key press needed in response to "Press any key to continue." NEWDISK will be the VOLUME ID. N is the response to the prompt, "Format another (Y/N)?". ^Z is the EOF marker. Next, place the following line in the batch file where you would normally put the FORMAT command: TYPE RESPONSE|FORMAT A:/V The TYPE command would normally display the contents of RESPONSE on the screen, but the | symbol instead pipes the contents of RESPONSE to the inputs in the FORMAT command, so they answer the prompts that would normally be answered from the keyboard. This same method can be used to answer the "Are you sure?" prompt, which comes up if you enter ERASE*.*. The keyboard inputs in these DOS commands are safety features that help prevent accidental erasures by letting the operator verify the command entry before it executes, so be sure to use them with great care. Editor's Note: This trick works wonders automating batch files and speeding up things in general. For instance, if you're performing lots of DISKCOPYs, you can load the disk to be copied onto a RAMdisk and then write a batch file to DISKCOPY to A: and then B: and then go back to the beginning and copy A: again - all without having to enter lots of Y's to continue. This lets you replace copied disk with raw ones in rapid succession. This also lets you do such nasty things as putting DEL*.* commands in places that don't need confirmations. If you're putting together a big batch file with lots of commands that would ordinarily need responses, be sure the responses are synchronized to the questions. If a complex batch file encounters an error, you could end up confirming some very destructive commands. You can do serious damage, even with simple files. For example, if you create a file called TROUBLE with just a Y and carriage return in it, then type: TYPE TROUBLE|DEL*.* your disk suddenly turns blank. If you do try this, you can replace all the files with DEBUG by using the L command to load in the directory and then change all the hex E5's back to their original characters.