Batch File Library

Batch File Library

          If you've been wanting to combine all those little batch files into a larger single bat file, here's a tool you'll find helpful.  When this file is typed in, save it under three names BAT.LIB, BAT.BAK, & BAT.BAT.  It's important to have the BAT.BAK file, since the program BAT.BAT alters itself and BAT.LIB.  BAT.BAK is square one when you want to start over.

          Before I begin to explain this, here's some tips and/or warnings:
     1) Backup all the batch files you're going to add to the library on floppy.
     2) Unless you KNOW what's going on, include only harmless batch files. Don't include FORMAT, etc.
     3) Check your batch files for labels. This program turns all the batch files into sections identified with a label. The label begins with a ":". If there's duplicate labels, you could be surprised what happens. If you don't understand batch files, don't include any with labels.
     4) Check your batch files for recursive calling, i.e. a batch file calling itself. When adding this type of batch file to the library, modify the batch file call to include a GOTO before it.
          If you have an executable file with the name BAT (BAT.COM or BAT.EXE), you can select another name, but be sure to make the change in the following files as well.

BAT.BAK, BAT.LIB, BAT.BAT

     ECHO OFF
     IF %1.==R. GOTO RUN
     IF %1.==A. GOTO CHECK
     GOTO PARM
     :RUN
     SHIFT
     SHIFT
     IF %0.==. GOTO PARM
     GOTO %0
     :CHECK
     IF EXIST BAT.LIB GOTO ADD
     ECHO BAT.LIB Library missing
     GOTO VEND
     :ADD
     SHIFT
     SHIFT
     IF %0.==. GOTO PARM
     REM *** BUILDING TEMPORARY FILES ***
     ECHO :%0 > BAT.TMP
     COPY BAT.TMP+%0.BAT BAT.TMP
     ECHO GOTO VEND > BAT.END
     ECHO :VEND > BAT.VND
     REM *** UPDATING LIBRARY ***
     COPY BAT.LIB+BAT.TMP+BAT.END BAT.LIB
     REM *** UPDATING MASTER BATCH FILE ***
     COPY BAT.LIB+BAT.VND BAT.BAT
     REM DEL BAT.TMP
     REM DEL BAT.END
     REM DEL BAT.VND
     REM DEL %0.BAT
     GOTO VEND
     :PARM
     ECHO Format is:
     ECHO BAT A batchfile       to add BAT file to library
     ECHO BAT R batchfile       to run BAT file from library
     ECHO      (don't include the .BAT extension)
     ECHO For example:
     ECHO BAT A clear    to add the file CLEAR.BAT to library
     GOTO VEND

          This little system works on the assumption that you've already got a series of batch files you want to combine into one.  To add a batch file enter the command:
             BAT A batchfile
where batchfile is the name of the batch file, WITHOUT the .BAT extension.  As an example, let's create a batch file, CLEAR.BAT, and then add it to the library.

CLEAR.BAT

     REM TEST FILE TO ADD TO BAT.BAT
     CD
     PAUSE
     CLS            

ADDING A BATCH FILE

          To add CLEAR.BAT to the library, enter the command:

                 BAT A CLEAR

     The display will show the following actions being taken:

     A>ECHO OFF
     BAT.TMP
     CLEAR.BAT
         1 File(s) copied
     BAT.LIB
     BAT.TMP
     BAT.END
         1 File(s) copied
     BAT.LIB
     BAT.VND
         1 File(s) copied

     If you then examine the BAT.LIB, you will notice that the following lines have been added.
     :CLEAR
     REM TEST FILE TO ADD TO BAT.BAT
     CD
     PAUSE
     CLS
     GOTO VEND

RUNNING A BATCH FILE

          To run this file from the BAT library, use the command: BAT R CLEAR.  Execution will be transferred to the label previously used as the name of the batch file.  To prevent execution from falling through to the next label when the batch files commands are completed, GOTO VEND directs execution to the end of the file, thus ending it.

 

Technical Notes. . .or How it Works

          If you've used batch files you can probably pick up on the syntax of the program.  Here's a few notes on the less familiar commands and formats.  The %n symbols indicate the command line parameters.  The parameters are separated by spaces and assigned (by DOS), the numbers 0 through 9.  The command BAT A CLEAR is initially broken down to indicate
     BAT = %0; A = %1; and CLEAR = %2.
          The IF test in DOS is somewhat limited.  There must be something on both sides.  If there's nothing assigned to the %1 in the line IF %1==R GOTO RUN, you will get a syntax error.  To prevent this, a period is added to each side; thus
     IF %1.==R.  GOTO RUN.
          GOTO directs execution of a batch file to the designated label.  A label is identified with the ":" in front of the word.  Thus, the initial section of the batch file determines if you want to ADD or RUN a file, or, if neither, that you need a reminder of the syntax.
          Before attempting to add a program to the library, there is a check to see if the library BAT.LIB is available.  If not, the program is terminated.
          Following the ADD label first, we come to another less used feature of batch file programming, SHIFT.  SHIFT moves the parameters over one place.  Where BAT was initially %0, and CLEAR was initially %2, following two SHIFTs, BAT is cleared from the parameter list, and CLEAR is now %0.  While it may seem just as easy to use %2 instead of SHIFTing parameters, there may be circumstances when a batch file added to the library uses additional parameters.  SHIFTing puts them in the order they would be if the individual batch file were used.

          Another check of syntax will direct execution to the format reminder if you don't have a third parameter; i.e.  if you just enter BAT A.  This brings us to the actual addition of the file to the library.  As you may know, ECHO is the command that displays something on the screen.  This use of ECHO can also be redirected to create files.  Combining several of the features here, we can use the command ECHO :%0 > BAT.TMP to create the file BAT.TMP with the label:
     :CLEAR

 THE BUILDING PROCESS

          Putting it all together, we can start from here, rebuilding the original BAT.BAT so that it includes the new program.  Because of the ending label needed in the actual BAT.BAT, it is necessary to maintain a second copy of this file without the ending label in order to build with.  This building copy is BAT.LIB.  The building process goes like this.
     Create the label file
                 BAT.TMP     :CLEAR
     Copy the label & the batch file together
             BAT.TMP + CLEAR.BAT  = BAT.TMP
     Create a label terminator, so execution won't fall through
                 BAT.END     GOTO VEND
     Copy the labeled batch file, and label terminator to the end of the library
         BAT.LIB + BAT.TMP + BAT.END = BAT.LIB
     Finally create the executable batch file BAT.BAT
             BAT.VND + BAT.LIB = BAT.BAT

          While it is possible to concatenate two files, or appending to the end of a file by using either TYPE or ECHO, if the appended file has a terminating Control-Z, then the created file will not function properly past the mark.

CLEANING UP

          You will notice that this system creates several files for its use during execution.  The BAT.LIB file is updated each time, and should not be deleted.  The others may be. The file includes the lines for this cleanup operation.  To activate them, just remove the REMs from the REM DEL... statements.

SEVERAL LIBRARIES

          There are basically two options for maintaining several batch file libraries.  First, you could create separate
     working batch libraries.  This would indicate that you would have to have separate names for them, (i.e.  UTILITY.BAT), and maintain support files for each (i.e.  UTILITY.LIB).  If you do choose this path, make sure to make the required changes in the program.
          A second option would be to freeze each library, and rename it before starting on another.  In this case, some of the required modifications would be to eliminate the ADD option; rename the base library filename (from BAT.BAT to something else with .BAT extension), and make the name changes in the program.  If you want to change the syntax from BAT R CLEAR to BAT CLEAR, simply remove the line IF %1.==R...., and delete one of the shifts.  When this batch file library is frozen, start the next library with fresh BAT.LIB & BAT.BAT files by copying from BAT.BAK.
          Following is BAT.BAT with the option CLEAR, modified in this way and renamed BATU.BAT.  About 25 lines of maintenance code used for ADDing files has been eliminated.

          ECHO OFF
          SHIFT
          IF %0.==. GOTO PARM
          GOTO %0
          :PARM
          ECHO Format is:
          ECHO BATU batchfile       to run BAT file from library
          ECHO      (don't include the .BAT extension)
          ECHO For example:
          ECHO BATU clear           to run the file CLEAR.BAT
          GOTO VEND
          :CLEAR
          REM TEST FILE TO ADD TO BAT.BAT
          CD
          PAUSE
          CLS
          GOTO VEND
          :VEND

WRITING LIBRARIES

          Here I've considered how to add previously written batch files to a library.  If you are considering using a library of batch files but haven't written the batch files yet, you can just as easily write the library directly.  If you choose this path you will be adding to your understanding of batch file programming and to your personal capabilites.  Creating a library directly would consist of the following steps and commands.

     1) Shift the first parameter off
          SHIFT
     2) Check for the existance of a command
          IF %0.==. GOTO PARM
     3) Transfer execution to the designated label
          GOTO %0
     4) Include an error message as a reminder of syntax
          :PARM
          ECHO Format is:
          ECHO BATU batchfile       to run BAT file from library
          ECHO      (don't include the .BAT extension)
          ECHO For example:
          ECHO BATU clear           to run the file CLEAR.BAT
          GOTO VEND
     5) Add the commands you want to include, beginning with a label (preceded by ":"), and with the last line GOTO VEND.
     6) Include the last line :VEND.

          If your options will themselves take options, be sure to include the parameters in the program call.  For example, if the program MWORD, may be called to edit the file DIARY, as in APPL MWORD DIARY, your batchfile command should be MWORD %1.

          Below is a sample program APPL.BAT that includes two selections.

APPL.BAT

          SHIFT
          IF %0.==. GOTO PARM
          GOTO %0
          :PARM
          ECHO Format is:
          ECHO APPL batchfile       to run BAT file from library
          ECHO      (don't include the .BAT extension)
          ECHO For example:
          ECHO APPL clear           to run the file CLEAR.BAT
          GOTO VEND
          :MWORD
          CD MSWORD
          MWORD %1
          CD \
          GOTO VEND
          :123
          CD LOTUS
          123
          CD \
          GOTO VEND
          :VEND

ENHANCEMENTS

          Whether you developed a library with the library program or created it directly, there may be items you could include to improve clarity.
          When I started developing the batch file library program I used the syntax used by XEQ, and most archiving programs, where A means ADD to the library, and R means RUN.  The batch file language limits the functionality of the program to this.  If you would like to add a list of the batch files added to the library so that it is displayed with the L selection, you will need to manually add the lines as in the following example.

     IF %1.==L. GOTO LIST
     :LIST
     ECHO Valid options in this library are:
     ECHO MWORD; 123;...
     GOTO VEND
  
          This listing could also be included as a part of addition process in the :PARM Program format message, but would require that you create a new working file for the parameter list and append the name of each new batch file to  the end of it.  Please note again that each selection with a label is separated from the next with the command GOTO VEND. If you forget it, program flow will fall through to the next selection.
          Execution of a null "QUIT.BAT", instead of GOTO VEND would also terminate execution, but would require you have the QUIT file in the path. Attempting to execute a QUIT when the file doesn't exist would cause DOS to report "Bad file or command" and fall through to the next selection.
          Unless you have a 386/20, you have probably noticed how slow batch files with many labels runs.  Not very fast.  In order to balance the versatility of batch files with their speed you'll probably be maintaining several libraries.  An improvement would be to have a program that could search a library and create a short batch file and execute that. Unfortunately, the DOS batch facility is not quite that versatile.  You would have to move on to a richer language.

No feedback yet
Leave a comment

You must be a member of this blog to comment. Log in now!

If you have no account yet, you can register now...
(It only takes a few seconds!)