home *** CD-ROM | disk | FTP | other *** search
/ Current Shareware 1994 January / SHAR194.ISO / dos_util / pcl60a.zip / PART3.EXE / BATCH.TUT next >
Text File  |  1993-07-13  |  51KB  |  1,051 lines

  1.       
  2.        ----------------------------------------------------------------
  3.  
  4.               BATCH FILES FOR EFFICIENCY - GETTING REAL WORK DONE 
  5.  
  6.        ----------------------------------------------------------------
  7.  
  8.        Batch files are one of the hidden treasures within your 
  9.        computer. Let's face it, learning and using DOS commands is a 
  10.        test of patience, memory and stamina. Batch files are the secret 
  11.        weapon which can boost your computer into the fast lane of work 
  12.        productivity. 
  13.        
  14.        Batch files are small software programs which you can prepare in 
  15.        the space of only five or ten minutes which automate a variety 
  16.        of tasks and customize the computer to your work style. The 
  17.        bottom line is that batch files can preserve your sanity in the 
  18.        face of arcane, easily-forgotten DOS commands to provide you 
  19.        with a menu system for your hard drive, transfer and backup 
  20.        files, provide security, start other software programs, activate 
  21.        your printer and much more. 
  22.        
  23.        Amazingly, batch files require that you become familiar with 
  24.        only eight commands in addition to the normal DOS commands. 
  25.        Working with batch files means you are programming in the most 
  26.        literal sense - let's take a tour of the small miracles called 
  27.        batch files . . . 
  28.        
  29.        A batch file is little more than a list of DOS commands plus 
  30.        eight special batch commands. These commands are stored on your 
  31.        floppy or hard disk in an ordinary text file such as produced by 
  32.        your word processor or text editor. If you can type a letter to 
  33.        a friend, you can prepare a batch file! 
  34.               
  35.        Several time-saving batch file examples which provide real work 
  36.        power to your everyday computer problems are presented at the 
  37.        end of this tutorial. We will also examine the eight basic batch 
  38.        file commands as well as the AUTOEXEC.BAT file which starts your 
  39.        computer each morning. In addition we will present some simple 
  40.        tricks for managing your printer with batch files.
  41.           
  42.        One way to think about a batch file is that it takes the 
  43.        place of your keyboard and issues commands one after another 
  44.        until it reaches a conclusion. Batch files operate line by line 
  45.        and are read directly from the disk which makes them a little 
  46.        slow, but nevertheless useful and flexible. You can eliminate 
  47.        repetitious keyboard tasks by using batch files. Let's look at a 
  48.        short batch file . . .
  49.           
  50.        Each line of a batch file contains one instruction or operation 
  51.        per line which the computer is to perform. Below is the listing 
  52.        of a simple batch file example. Don't worry about understanding 
  53.        it yet, simply note that each instruction is a DOS command on a 
  54.        separate line. The list in the left column is the actual batch 
  55.        file, while the explanation in the right column is NOT part of 
  56.        the batch file, only a helpful column of comments. 
  57.  
  58.         EXAMPLE BATCH FILE                EXPLANATION
  59.                |                               |
  60.               date                         date displayed 
  61.               time                         time displayed
  62.               ver                          DOS version displayed
  63.               dir a:/p                     directory of a: floppy 
  64.                                            displayed with a pause
  65.  
  66.        The primary use of batch files is to automate sequences or 
  67.        instructions which you use frequently. A batch file always has 
  68.        the extension BAT. A batch file might for instance be named
  69.        MENU.BAT, CAR.BAT, INSTALL.BAT or MONEY.BAT.
  70.  
  71.        Each line in a batch file is a separate command and is performed 
  72.        in sequence as if you had typed in the command from your 
  73.        keyboard at the DOS prompt. In addition to the usual DOS 
  74.        commands, batch files can also contain additional special 
  75.        commands to provide truly sophisticated program structures which 
  76.        include decision branching and even repetitions of commands. 
  77.        In addition, batch files may have special parameters or inputs 
  78.        passed to them at the time you run the batch file from the DOS 
  79.        command line or prompt. 
  80.        
  81.        A batch file is run or started by typing the file name without 
  82.        the extension. This of course also applies to files ending with 
  83.        file extensions EXE or COM as well as BAT.
  84.  
  85.        Example: A>hello     (Then pressing enter or return key)
  86.        This starts the file   hello.bat hello.com or hello.exe
  87.  
  88.        Example: C>whoops    (Then pressing enter or return key)
  89.        This starts the file  whoops.exe whoops.bat or whoops.com
  90.        
  91.        There are several ways to abort or terminate any batch file in 
  92.        progress. 1) Issue the break command which uses the two key 
  93.        combination CONTROL-BREAK (hold down the control or CTRL key 
  94.        then press the break key) or you can 2) Tap CTRL-SCROLL LOCK 
  95.        keys or 3) Tap CTRL-C keys. 
  96.  
  97.        There are many ways to prepare a batch file, all of which use 
  98.        simple methods of text editing or word processing:  1) Use the 
  99.        DOS COPY CON (copy console) command. 2) Use the older EDLIN line 
  100.        editor available within older versions of DOS. 3) Use the newer 
  101.        DOS EDIT text editor available in DOS version 5.0  and 6.0. This 
  102.        is probably the most convenient. 4) Or use any word processor 
  103.        (e.g., Microsoft Word, Wordperfect, PC-Write) whose output has 
  104.        been set to ASCII or pure text output - many word processors use 
  105.        a "save as" file option to select pure ASCII output. See your 
  106.        word processor reference book index under ASCII file saving. 
  107.        
  108.        Let's prepare a batch file: 
  109.  
  110.        First make sure you have a formatted disk in your disk drive and 
  111.        DOS is displaying a DOS prompt such as A> or C>. We need a disk 
  112.        in order to save our batch file.
  113.        
  114.        We will be using the command COPY CON (copy data from the 
  115.        CONsole) command. We could also use any ASCII (plain english) 
  116.        text word processor (e.g., Wordperfect) or even EDLIN on your 
  117.        DOS disk. Note that you can use either upper or lower case to 
  118.        prepare batch files (capitals or small letters.) Using COPY CON 
  119.        is like using a small typewriter to prepare your batch file. 
  120.  
  121.        Type the following list carefully at the DOS prompt:
  122.  
  123.        copy con blink.bat     (press enter - cursor skips to new line)
  124.        echo Hello there       (press enter)
  125.        ver                    (press enter)
  126.        date                   (press enter)
  127.        dir/p                  (press enter)
  128.        ^Z                     (press F6 OR your can press control key 
  129.                               AND Z key, then press enter)
  130.  
  131.        When done, you'll have prepared a batch file of DOS commands 
  132.        named blink.bat. Run the batch file by typing this at the DOS 
  133.        prompt: 
  134.                           blink    (then press enter key)
  135.        
  136.        Although using the COPY CON method method of preparing a batch 
  137.        file works, users of DOS 5.0 and 6.0 will find it more 
  138.        convenient to use the built in DOS "miniword processor" called 
  139.        EDIT. Just type the command EDIT  <then press enter/return key> 
  140.        to load EDIT. Then use the convenient popdown menus within EDIT 
  141.        to create a file or load a file from the disk. 
  142.  
  143.        WARNING! Be careful when preparing batch files since you will 
  144.        automatically overwrite and destroy any PREXISTING batch files 
  145.        of the same name! It may be better to make a backup copy of the 
  146.        existing batch file (or rename it temporarily with the REN 
  147.        command) and then proceed. A classic beginner mistake is to 
  148.        tinker with the crucial AUTOEXEC.BAT file without saving a 
  149.        backup copy first! If you destroy AUTOEXEC.BAT with your 
  150.        tinkering, you can replace it with your known good copy. More 
  151.        about AUTOEXEC.BAT later in this tutorial. 
  152.  
  153.        We could also have named the batch file above hello.bat or 
  154.        info.bat rather than blink.bat by changing the first line we 
  155.        typed, but for simplicity we'll stick with blink.bat which does 
  156.        the following chores: Print "hello there" on the screen, then 
  157.        type the DOS version in use then display date and finally 
  158.        produce a directory listing with pause after each screenful. At 
  159.        this point the batch file ends and returns you to DOS. In the 
  160.        first line we use COPY CON as our small word processor to begin 
  161.        construction of the batch file named blink.bat. In the last line 
  162.        the ^Z means end of batch file preparation - exit back to DOS 
  163.        and save the file on disk. 
  164.  
  165.        Another example batch file for you to try, let's call it F.BAT
  166.        This is a reminder that F.BAT refers to formatting a disk:
  167.  
  168.        echo off                                        
  169.        copy con f.bat                                                 
  170.        cls                                                            
  171.        pause                                                          
  172.        format b:                                                      
  173.        echo all done                                                  
  174.  
  175.        Notice that here I have omitted the COPY CON command to start 
  176.        file preparation and the F6 to end file preparation. Use the 
  177.        COPY CON method described above if you wish or whatever word 
  178.        processing software is available to construct the file. 
  179.        
  180.        This batch file (activated by typing f then enter) will clear 
  181.        the screen then prepare to format a blank disk in b: drive. 
  182.        NOTE: you MUST have FORMAT.COM, the DOS formatting utility, on 
  183.        the same disk as the batch file, f.bat - remember that format is 
  184.        an EXTERNAL command and f.bat will try to find FORMAT.COM. 
  185.        
  186.        After the batch file has formatted the disk it prints "all done" 
  187.        on the screen. So instead of LOTS of keystrokes to format a 
  188.        disk, you just tap "F" then hit enter and the batch file runs. 
  189.        See how we are saving keystrokes - that's one of the purposes of 
  190.        a batch file! We will discuss the new ECHO command a little 
  191.        later in this tutorial. 
  192.        
  193.        A reminder: Ctrl-Break or Ctrl-C key combination will halt 
  194.        any batch file operation if you wish.
  195.  
  196.        The next batch file might be used to backup word processing data 
  197.        files from your hard drive onto a floppy disk. Let's make an 
  198.        initial assumption that your word processing documents are 
  199.        stored on your hard drive in the subdirectory C:\DOC. You could 
  200.        name this backup batch file B.BAT and when you need to backup 
  201.        simply type B (then press enter) at the DOS prompt. We've 
  202.        omitted the copy con command at the top of the file and the ^Z 
  203.        at the end of the file since you already know how to start and 
  204.        end a batch file from previous examples. Note the new commands 
  205.        we are using: REM, ECHO and PAUSE which we will discuss shortly. 
  206.  
  207.        ECHO OFF
  208.        ECHO This batch file backs up DOCUMENT files to disk B:
  209.        ECHO READY TO BACKUP. 
  210.        PAUSE                                                                  
  211.        COPY C:\DOC\*.* B:
  212.        ECHO All done!                                                          
  213.  
  214.        The line which does most of the work is COPY C:\DOC\*.* B: 
  215.        which translates as "copy all files from C:\DOC subdirectory 
  216.        and transfer them to B: drive."
  217.  
  218.        One batch file can start or call another, but the original batch 
  219.        file cannot usually be returned to - you must continue on within 
  220.        the second batch file. For example, you could have one batch 
  221.        file start another batch file.
  222.  
  223.        If a batch file contains a typing or syntax error in any of its 
  224.        commands, the computer will stop execution at that point and 
  225.        return you to DOS which remembers which disk contains the batch 
  226.        file and the drive it was in. If you remove the original disk, 
  227.        DOS will ask you to replace it so it can finish executing the 
  228.        batch file. Batch files execute one step at a time from the disk 
  229.        and NOT from RAM memory. This disk-based nature of batch files 
  230.        make them a little slow, but they get the work done in 
  231.        reasonably short order for most people.
  232.  
  233.        Several books and power user tricks should also be mentioned 
  234.        regarding batch files before we move on . . .
  235.  
  236.        A superlative book on batch files you might wish to investigate 
  237.        is MOS-DOS Batch File programming by Ronny Richardson, 1988,
  238.        Wincrest Books.
  239.       
  240.        You should also investigate the SEBFU (Scanlon Enterprises Batch 
  241.        File Utilities) software package which is a series of small 
  242.        batch file utilities which offer an improvement over the 
  243.        standalone DOS batch file programming language. SEBFU allows the 
  244.        user to produce subtle, powerful batch files and includes an 
  245.        excellent tutorial about using batch files for productivity. If 
  246.        you wish to try SEBFU, the shareware version, contact Scanlon 
  247.        Enterprises, 38354 17th ST E #C, Palmdale, CA 93550 Telephone 
  248.        (805) 272-4827. Include five dollars for shipping and handling. 
  249.        
  250.        Special batch file COMPILER utilities exist which speed 
  251.        execution of batch files and make them run from RAM memory 
  252.        rather than disk. Most computer clubs and BBS system carry these 
  253.        batch file compilers. One popular batch compiler is named 
  254.        BAT2EXEC and was produced by PC Magazine several years ago. 
  255.        Batch files will FLY once they have been compiled and run 
  256.        from memory rather than disk. Another batch file speedup trick 
  257.        uses a "ramdisk" as follows.
  258.  
  259.        Remember the DOS VDISK command in our second DOS tutorial? Many 
  260.        DOS experts put commonly used batch files in a virtual or RAM 
  261.        disk in memory where a batch file runs quickly. This is one 
  262.        trick which can turbocharge batch file operations. 
  263.  
  264.        ----------------------------------------------------------------
  265.  
  266.           AUTOEXEC.BAT FILE BASICS - THE WAKEUP CALL TO YOUR COMPUTER 
  267.        
  268.        ----------------------------------------------------------------
  269.       
  270.        The AUTOEXEC.BAT file starts your computer exactly the way you 
  271.        want. It allows you to customize the machine to your liking as 
  272.        the computer comes to life. You can cause the AUTOEXEC.BAT file 
  273.        to print a startup menu of choices, load one particular program, 
  274.        execute another batch file or other useful tasks. The 
  275.        AUTOEXEC.BAT file is the first file DOS runs after loading 
  276.        itself and configuring the computer. The AUTOEXEC.BAT file must 
  277.        be on the same disk as DOS when the computer starts. 
  278.  
  279.        The AUTOEXEC.BAT file is a special batch file which MUST be 
  280.        placed in the main or root directory of a disk to function 
  281.        properly. 
  282.  
  283.        An AUTOEXEC.BAT file can always be modified, enlarged, edited, 
  284.        or deleted later as you wish. Sometimes it is useful to have 
  285.        several AUTOEXEC.BAT files. Each on a different startup disk to 
  286.        operate different programs! An AUTOEXEC.BAT file, like all batch 
  287.        files, can be modified with any word processor, DOS EDIT or 
  288.        EDLIN text editor. 
  289.  
  290.        Before tinkering with your AUTOEXEC.BAT file, make sure you do 
  291.        not accidentally over-write or destroy your current AUTOEXEC.BAT 
  292.        file. If necessary, rename your current AUTOEXEC.BAT file (using 
  293.        the rename or REN command) and make a new file while saving the 
  294.        old one "just in case." Never edit files on your original DOS disk, 
  295.        work on a copy! This wise advice applies to ANY computer file.
  296.        Save a backup copy; never work on the original.
  297.  
  298.        Examine the next batch file:
  299.  
  300.        copy con AUTOEXEC.BAT              (press enter)
  301.        123                                (press enter)
  302.        ^Z                                 (press enter)
  303.  
  304.        This means (first line) create a file named AUTOEXEC.BAT as 
  305.        typed from the keyboard or con (console). Then (second line) 
  306.        start program named 123. The (final line) end of batch file 
  307.        preparation - stash it on the disk. Since the first and last 
  308.        lines prepare the batch file, this program really has only one 
  309.        line whose purpose is to start a specific program (123.EXE) each 
  310.        time the computer is turned on.
  311.  
  312.        When finished you'll see a file named AUTOEXEC.BAT on your 
  313.        directory listing screen which contains automatic startup 
  314.        instructions. If this file were placed on your main DOS disk it 
  315.        would try to start a program such as 123.EXE if such a program 
  316.        existed there. And since it is AUTOEXEC.BAT this would be the 
  317.        first file run each morning when you turn on your computer.
  318.  
  319.        You can also start the AUTOEXEC.BAT by typing autoexec and then 
  320.        pressing enter. To take a "peek" at the contents of an 
  321.        AUTOEXEC.BAT file (or any bat file) simply use the type command. 
  322.        Remember to use Ctrl-S key combination to pause the screen if 
  323.        the display flashes by too quickly. 
  324.  
  325.        Example: C>type AUTOEXEC.BAT       (display file contents)
  326.        Example: A>type b:AUTOEXEC.BAT     (display file on the B: drive)
  327.        Example: C>type AUTOEXEC.BAT>PRN   (display file contents on 
  328.                                            printer)
  329.        
  330.        Here is another AUTOEXEC.BAT file, this time from a hard drive 
  331.        computer. It provides a higher degree of control and direction 
  332.        that a computer user might need for hard drive customization. 
  333.  
  334.        path \dos;\reflex;\wp;\util;\doc;\nor;\bat                     
  335.        prompt $P$G                                                    
  336.        cpu n                                                          
  337.        verify on                                                      
  338.        blank                                                          
  339.        mode bw80,r                                                    
  340.        dispclk                                                        
  341.        type menu.txt                                                  
  342.  
  343.        Let's examine this more complicated AUTOEXEC.BAT file in greater 
  344.        detail: 
  345.  
  346.        The first line after establishes a path command to help DOS 
  347.        search every subdirectory on the hard disk -you don't have to 
  348.        switch around to different areas of the disk, DOS will search 
  349.        for you since it knows the various subdirectory "paths" to take. 
  350.  
  351.        The second line alters the cursor prompt to always display your 
  352.        current location and subdirectory. Instead of seeing C> you view
  353.        a more informative C:\DOCS> for example.
  354.  
  355.        The third line is a reference to the speed the computer will 
  356.        operate at and is a unique command to a particular brand of 
  357.        machine (cpu n means start the central processing unit chip at 
  358.        normal speed.) Cpu is really CPU.COM, an external file which 
  359.        sets the computer's processing speed. Your DOS disk may or may 
  360.        not contain the file CPU.COM. This highlights the ability of the 
  361.        AUTOEXEC.BAT file to start or load other programs and is 
  362.        very useful!
  363.  
  364.        The fourth line turns on the verify function for file copying. 
  365.  
  366.        The next line instructs the DOS mode function to switch to black 
  367.        and white display, 80 columns wide and shift one column to the 
  368.        right for alignment. We are setting the hardware the way we 
  369.        wish. We could also configure the modem or printer with the mode 
  370.        command.
  371.  
  372.        Next we ask DOS to tell us the time and date. Run the program 
  373.        DISPCLK.COM, an external program stored on disk.
  374.   
  375.        The final line instructs DOS to type to the screen a text file 
  376.        containing a simple menu for the monitor to display. Menu.txt
  377.        probably gives us choices of programs and thus calls other batch 
  378.        files. 
  379.  
  380.        ---------------------------------------------------------------- 
  381.  
  382.                           BATCH FILE COMMANDS AND USE 
  383.  
  384.        ---------------------------------------------------------------- 
  385.  
  386.        In addition to the normal DOS commands, batch files have eight 
  387.        special subcommands. At the end of this section we will provide 
  388.        some interesting batch files which you can use or edit on your 
  389.        computer. 
  390.        
  391.        The special batch commands are: 
  392.  
  393.        --- REM --- The rem command sends a message to the screen or 
  394.        simply documents or notes a part of a batch file's operation. 
  395.        You should use REM extensively to document long detailed batch 
  396.        files so you can revise things and locate portions of the 
  397.        program if you decide to change the batch file later. Remarks 
  398.        can be up to 123 characters long. REM does not cause any 
  399.        operation, it merely documents what you want to say or do.          
  400.  
  401.        Example: C>REM this is the location of menu operations 
  402.  
  403.        In DOS 2.0 the REM command could be replaced with a period or 
  404.        dot, but this is not true in DOS 3.0 and above. 
  405.  
  406.        Example:  C>. this is the location of menu operations 
  407.  
  408.        --- PAUSE --- Stops batch file execution on a temporary basis 
  409.        until you press a key. Thus you can pause a batch file and do 
  410.        some operation (perhaps changing a floppy disk) and then 
  411.        continue when you strike a key. Very useful.
  412.  
  413.        Example: B>PAUSE   
  414.        Example: B>PAUSE This is an optional message, pardner! 
  415.  
  416.        In the first example, no message is displayed.
  417.  
  418.        --- ECHO --- Turns display listing of commands on/off. It can 
  419.        also send a message to the screen. It is frequently turned off 
  420.        to remove excessive screen messages. Normally, with ECHO on, 
  421.        screen messages are sent to the screen which can be distracting. 
  422.        To suppress them use the first example. To restart the messages 
  423.        use the second example. To add a message with the ECHO command 
  424.        see example three. REM or remark command can also send a message 
  425.        to the screen but NOT with ECHO turned off! 
  426.  
  427.        Example:   A>ECHO OFF 
  428.        Example:   A>ECHO ON 
  429.        Example:   A>ECHO It's raining cats, dogs and computers
  430.        Example:   A>@ECHO (don't display this particular line)
  431.        
  432.        --- PARAMETERS AND MARKERS ----  This is NOT a batch file 
  433.        command like ECHO or PAUSE. 
  434.  
  435.        Instead parameters are additional pieces of information or 
  436.        "modifiers" which follow DOS commands. 
  437.  
  438.        Example:   C>format b:/s     
  439.  
  440.        In the above, format is the command while b: and /s are the 
  441.        parameters. Parameters modify the basic operation of a DOS 
  442.        command but are not required by the command to operate. A batch 
  443.        file can also accept parameters such as a word, filename, 
  444.        symbol, drive letter or any useful character or group of 
  445.        characters! 
  446.  
  447.        Markers placed inside the batch file listing signify which 
  448.        parameter goes where. Markers are made from a percent sign (%) 
  449.        and a single digit between 0 and 9 for a total of ten markers 
  450.        available (remember, zero is a number too.) Here are the ten 
  451.        markers: 
  452.  
  453.        %0    %1    %2    %3    %4    %5    %6    %7    %8    %9 
  454.  
  455.        Let's use an example. Pretend that DOLITTLE.BAT is on your 
  456.        floppy. Within its listing of commands there might be this 
  457.        single line: 
  458.  
  459.        ECHO %0 %1 %2      (ECHO shows messages on the monitor) 
  460.  
  461.        If at the DOS prompt you typed:  
  462.        
  463.        B>DOLITTLE fancy pants  (then press enter) 
  464.  
  465.        Your screen would show the following:   
  466.                   
  467.                           ECHO DOLITTLE fancy pants. 
  468.  
  469.        In this case, %0 has taken on the value at the start of the DOS 
  470.        command which is the first word "DOLITTLE". Meanwhile %1 has 
  471.        become "fancy" and %2 is now pants. 
  472.  
  473.        Looking at this another way: 
  474.  
  475.                         DOLITTLE   fancy  pants 
  476.                          |          |       | 
  477.                  ECHO    %0         %1      %2    
  478.  
  479.        Let's try a more useful example. Pretend you had a large file of 
  480.        word processing files containing bills you have to pay from time to 
  481.        time. 
  482.  
  483.        You need to look up bills or amounts in the file accounts.txt 
  484.        which is in plain ASCII (english) text from your word processor. 
  485.        
  486.        The DOS FIND utility can search large files for specific words, 
  487.        strings or characters. The general format for the FIND command 
  488.        is: FIND "text" filename. FIND is located in the file FIND.COM
  489.        on your DOS disk and must be present with the batch file to be
  490.        used.
  491.  
  492.        A simple batch file possibly named GET.BAT could do this: 
  493.        
  494.        ECHO OFF
  495.        ECHO searching for data . . . . 
  496.        FIND "%1" %2 
  497.        ECHO Finished, boss 
  498.  
  499.        Start the batch file get.bat with search data like this: 
  500.  
  501.        C>get grocery accounts.txt    (first word starts get.bat, second 
  502.        word is the item to search for, third item is the file to 
  503.        search.) 
  504.        
  505.        As a result, you will get a report of the line where the word 
  506.        "grocery" is found within the file accounts.txt. This could also 
  507.        be used to search a telephone list or list of employee names and 
  508.        addresses. A powerful idea for a short batch file! 
  509.  
  510.        --- GOTO ---  Jumps to a labeled set of commands within the 
  511.        batch file. The general format for the command is    GOTO LABEL 
  512.        where LABEL is a line in the batch file which must start with a 
  513.        colon (:) followed by a name up to eight characters long. 
  514.  
  515.        A simple, but useless batch file illustrates the GOTO command by 
  516.        looping around in circles doing the same task endlessly.            
  517.                                                             
  518.        Example listing for batch file: 
  519.  
  520.        ECHO OFF
  521.        :kitty 
  522.        ECHO watch this fill your screen over and over, folks 
  523.        GOTO kitty 
  524.  
  525.        Note! On some versions of DOS it is necessary to include one blank 
  526.        line at the end of this file. In the above example, just press 
  527.        Enter/Return key one extra time after the line "GOTO kitty"
  528.        and then save the batch file.
  529.  
  530.        The above batch file will continue to print the remark line over 
  531.        and over since it always returns to the start. Tap Ctrl-Break to 
  532.        stop this silliness. The true usefulness of the GOTO command is 
  533.        best understood by allowing the GOTO within a batch file to 
  534.        transfer control elsewhere within its listing rather than to the 
  535.        line immediately next in sequence. You can thus cause varying 
  536.        useful results depending on a conditions present. Choices and 
  537.        different outcomes are a trademark of savvy batch file use.
  538.  
  539.        --- IF ---  Allows conditional operation of a command. This is a 
  540.        fancy way of saying you can cause a batch file to make decisions 
  541.        based on a logical condition or input then do something. The 
  542.        usual syntax of the IF command is IF CONDITION COMMAND. Let's 
  543.        take this apart and examine the concept. 
  544.  
  545.        In the situation IF CONDITION COMMAND: 
  546.  
  547.        COMMAND is any normal DOS or batch file command and CONDITION is 
  548.        one of three possible tests that yield true or false.         
  549.  
  550.        Example:  IF %1==w GOTO dog        (we'll explain this in a bit) 
  551.        Example:  IF %3 == 80 MODE BW80    (we'll explain this in a bit) 
  552.  
  553.        The three possible tests are: 
  554.  
  555.        1. The ERRORLEVEL condition (i.e., a specific number is found). 
  556.        2. The STRING COMPARISON. (i.e., two strings are equivalent or 
  557.        not.) 
  558.        3. The FILE EXISTENCE condition. (i.e., if a file exists or not.) 
  559.  
  560.        In true full-featured programming languages many other logical 
  561.        tests might be allowed, but for batch files these are the 
  562.        only three tests. Let's examine the three more closely. Then 
  563.        illustrate with an example.
  564.  
  565.        1. ERRORLEVEL is a number which tells DOS whether the last 
  566.        program run was successful. If so the errorlevel is zero (0) 
  567.        anything else above zero means unsuccessful. 
  568.  
  569.        2. STRING COMPARISON, the second conditional test, is always 
  570.        indicated in a batch file by double equals signs (==). A test is 
  571.        designated by the condition  IF string1 == string2. This is 
  572.        frequently used with parameters or markers such as:  IF %3 == 80 
  573.        MODE BW80. 
  574.  
  575.        3. In the final and third conditional test, FILE EXISTENCE, the 
  576.        usual format is IF EXIST d:filename.ext. which checks for a 
  577.        certain file on a certain drive. You can thus check for a 
  578.        certain disk or file before continuing the batch file process. 
  579.        Pathnames are not allowed (d:\slip\and\slide). 
  580.  
  581.        Let's try a batch file example to illustrate the use of STRING 
  582.        COMPARISONS to make a choice in how the batch file does its 
  583.        work. In a way, this is a menu program. Pretend you have two 
  584.        software applications. One is a word processor named WORD.EXE 
  585.        whose command to start is WORD and the other is a spreadsheet 
  586.        named LOTUS.EXE whose command is LOTUS to start. 
  587.  
  588.        If we prepared a simple batch file called go.bat whose listing is 
  589.        below, we could start one or the other program by using either 
  590.        the command:  
  591.  
  592.                      A>go w  (to start the word processor)  
  593.  
  594.                                    OR THIS:
  595.  
  596.                       A>go s  (to start the spreadsheet). 
  597.  
  598.        Notice how the "w" or "s" is picked up by the batch file and 
  599.        sends the program either one direction or the other in the 
  600.        example below. The remarks lines which begin with REM in the 
  601.        batch file give you a clue about the operation of the program 
  602.        but are not themselves commands. The end result of this batch 
  603.        file is a saving of keystrokes for frequently used software (the 
  604.        word processor and spreadsheet) and could be expanded to start 
  605.        many other software packages. 
  606.       
  607.        REM This batch file selects one of two choices based on input
  608.        REM The next line turns off screen echo to avoid screen clutter
  609.        ECHO OFF
  610.        REM Begin test for one of two choices
  611.        REM Next two lines use percent signs as markers for "w" or "s" keys 
  612.        IF %1==w GOTO dog
  613.        IF %1==s GOTO cat
  614.        REM Next line forces goto end if no match is made for w or s 
  615.        GOTO end
  616.        :dog
  617.        REM Next command starts word processor, WORD.EXE
  618.        WORD
  619.        GOTO end
  620.        :cat
  621.        REM Next command starts spreadsheet, LOTUS.EXE
  622.        LOTUS
  623.        GOTO end
  624.        :end
  625.        REM Next line switches to root directory and ends the batch file
  626.        CD\   
  627.        ECHO Batch file done, bye bye!
  628.        
  629.        --- SHIFT ---  Re-assigns the relationship of parameters to 
  630.        markers. It changes their values. And it does it in a very odd 
  631.        way . . . 
  632.  
  633.        Remember that there are only ten markers available to a batch 
  634.        file to hold the parameter values as we mentioned above. Here 
  635.        they are: 
  636.  
  637.        %0    %1    %2    %3    %4    %5    %6    %7    %8    %9 
  638.  
  639.        However you can raise the limit of 10 parameters in a batch file 
  640.        using the single word SHIFT. When this command is encountered in 
  641.        a batch file, all the parameter and marker pairings are shifted 
  642.        one unit to the left. Whatever was assigned to %0 is lost. 
  643.  
  644.        A diagram to visualize. Before a SHIFT command is issued the 
  645.        parameters and markers might be: 
  646.  
  647.                 %0    %1    %2    
  648.                  |     |     |
  649.                 dog   cat    computer
  650.  
  651.        After the SHIFT command we would see:
  652.  
  653.                 %0    %1          %2    
  654.                  |     |          |
  655.                 cat   computer
  656.  
  657.        Notice that dog is lost, %1 becomes computer and %2 is left 
  658.        vacant unless it takes a new parameter from %3 (if %3 had a 
  659.        parameter). The effects of the SHIFT command are wide ranging 
  660.        throughout the batch file and provide great flexibility and a 
  661.        range of parameters greater than ten values. 
  662.  
  663.        --- FOR..IN..DO ---    Allows iteration (repetition) of actions 
  664.        or commands. The command is similar to a FOR...NEXT...STEP loop 
  665.        programmers use. This command lets you repeat an action several 
  666.        times.
  667.  
  668.        The command is rather subtle and could be thought of as a three 
  669.        part command. The syntax is: 
  670.  
  671.        FOR %%Variable IN (Set) DO Command 
  672.  
  673.        Let's look more closely at the three parts: 
  674.  
  675.           FOR %%Variable       IN (Set)    DO Command
  676.           ==============       =======     ==========
  677.               |                    |           |         
  678.             part 1              part 2     part 3    
  679.  
  680.        Translating into English this means: FOR a certain batch file 
  681.        variable withIN a SET of filenames or commands DO a certain 
  682.        action. 
  683.  
  684.        The %%VARIABLE is a one-letter variable which must have a double 
  685.        %% prior to the letter to distinguish it from single % markers 
  686.        we have seen earlier. 
  687.  
  688.        The SET portion of the command is always in parenthesis as 
  689.        (SET). The SET represents filenames or DOS commands you want the 
  690.        %% variable to assume while the command is executing. A space is 
  691.        used between entries. Pathnames are never allowed but wildcards 
  692.        such as *.* are acceptable. If the SET contains DOS command then 
  693.        only the %%VARIABLE is used. 
  694.               
  695.        The COMMAND is a DOS command or batch subcommand. One or several 
  696.        of these commands will contain the %%Variable in it. 
  697.  
  698.        Let's try an example. Pretend by you want a batch file to 
  699.        present the DOS version then clear the screen and finally issue 
  700.        the directory. We could do this in three lines by: 
  701.        
  702.        VER
  703.        CLS
  704.        DIR/P
  705.        
  706.        However, with the command FOR..IN..DO we can do this in one 
  707.        line:
  708.  
  709.        FOR %%T IN (Ver cls Dir/P) DO %%T 
  710.  
  711.        Notice how each DOS command is separated by a space. ? and * are 
  712.        NOT allowed within any command within the SET. Use a colon : 
  713.        instead of a space within the set when passing parameters to 
  714.        programs. You can issue the FOR..IN..DO batch file subcommand at 
  715.        the DOS prompt by dropping one of the percentage signs  %  on 
  716.        the variable. Let's move on to some practical and fairly 
  717.        interesting examples . . . 
  718.  
  719.        ---------------------------------------------------------------- 
  720.  
  721.                       BATCH FILE PROJECTS FOR YOU TO TRY!
  722.  
  723.        ---------------------------------------------------------------- 
  724.  
  725.        The following batch files do real work and can teach you some 
  726.        fascinating principles. 
  727.        
  728.        To save typing each example, here's a shortcut: simply load this 
  729.        file, BATCH.TUT, from the PC-Learn disk or hard drive into your 
  730.        word processor. Next, delete the tutorial and batch files you 
  731.        don't need, keeping only the batch file lines you require. Move 
  732.        all lines of the batch file to the far left margin then save the 
  733.        batch file back to disk in plain ASCII text with a permanent 
  734.        batch file name such as CANDY.BAT, GO.BAT or PRINTER.BAT which 
  735.        you can easily remember. Feel free to change or add things to 
  736.        these batch files with your word processor since that's the 
  737.        point - batch files are flexible! 
  738.        
  739.        Note the liberal use of the remarks or REM lines in some batch 
  740.        files to explain how things work. If you like, delete all REM 
  741.        lines to save typing time, space and speed program execution! 
  742.  
  743.                       ---- Fast freespace batch file ----
  744.        
  745.        This is a short but useful batch file. It reports the amount of 
  746.        freespace on a disk by using the FIND command in DOS to search 
  747.        out the line containing the word "free" in the DOS DIR command.
  748.  
  749.                             Program name: TELLFREE.BAT
  750.        
  751.        ECHO OFF
  752.        CLS
  753.        ECHO CHECKING FOR FREE SPACE ON DISK
  754.        DIR | FIND "free"
  755.  
  756.        You could modify the last line to DIR A: | FIND "free" to locate 
  757.        the free space on the A: drive. The piping symbol | , discussed 
  758.        in a previous DOS tutorial illustrates how one command (DIR) can 
  759.        "pipe" its output into the FIND command. You MUST have the DOS 
  760.        file FIND.EXE on the same disk so that the DIR command can use 
  761.        it. The word "free" is case sensitive and must MOT be typed in 
  762.        capital letters since the FIND command is case sensitive. 
  763.        
  764.        Obviously, to use this small program, at the DOS prompt just 
  765.        type TELLFREE then press the return or enter key. You could also 
  766.        name this batch file T.BAT if you wanted to only type a 
  767.        single letter and save additional keystrokes. 
  768.  
  769.                       ---- Fast deletion batch file ----
  770.        
  771.        This is a short batch file with some powerful wrinkles for 
  772.        speedy file deletions. It could be modified in many ways.
  773.  
  774.                               Program name: DB.BAT
  775.        
  776.        ECHO OFF
  777.        CLS
  778.        ECHO Ready to delete ALL files on B: drive
  779.        ECHO Press control-break keys to abort or
  780.        ECHO any other key to continue
  781.        PAUSE
  782.        REM Following line does the deleting
  783.        ECHO Y | DEL B:*.*
  784.        ECHO DONE!
  785.  
  786.        This is an odd batch file using some powerful DOS secrets. Line 
  787.        four reminds us we are about to delete all files on the B: drive 
  788.        and gives us the chance to abort using the control-break key 
  789.        combination which works to abort all batch files. The pause 
  790.        command on line six halts operations and waits for our keypress. 
  791.        
  792.        Line eight shows the real power of DOS in a one line command 
  793.        which uses ECHO to pass the keystroke Y (meaning yes) via the 
  794.        pipe operation of DOS represented by the vertical bar |. This 
  795.        effectively means that the deletion of all files on B: drive 
  796.        represented by *.* will take place WITHOUT pausing for the 
  797.        traditional Yes/No request. The name of the batch file, DB.BAT 
  798.        reminds us that its use is to delete all files on B: drive or 
  799.        DB, for short! 
  800.               
  801.                       ---- Fast formatting batch file ----
  802.        
  803.        This batch file will speed your disk formatting. Its name 
  804.        F.BAT means format disks, and it gives you some choices and 
  805.        illustrates other batch file tricks.
  806.        
  807.        There are three ways to use it: for formatting A: drive, B: 
  808.        drive, or both A: and B: drives. Note the minimum number of 
  809.        keystrokes required and how the batch file determines your 
  810.        choice by using parameters. This batch file also requires a 
  811.        small text file called yes.txt which shows how a file can be 
  812.        used to redirect input to a DOS command in place of the 
  813.        keyboard. It is explained below. Be sure to prepare YES.TXT or 
  814.        F.BAT will not work!
  815.  
  816.        To start this batch file you MUST chose one of the following. 
  817.        The space between letter characters is important.
  818.  
  819.        To format only A: drive, at DOS prompt enter   F A
  820.        To format only B: drive, at DOS prompt enter   F B
  821.        To format both A and B drives, at DOS prompt enter   F AB
  822.        
  823.        In the above commands, the A, B or AB will be inserted into the 
  824.        batch file in the location of the symbol %1 as noted earlier in 
  825.        this tutorial.
  826.        
  827.                              Program name: F.BAT
  828.        
  829.        ECHO OFF
  830.        CLS
  831.        ECHO FORMATTING DISKS NOW! 
  832.        REM This batch file selects one of three choices based on input
  833.        REM Begin test for one of three choices
  834.        IF %1==A GOTO DOG
  835.        IF %1==B GOTO CAT
  836.        IF %1==AB GOTO MOUSE
  837.        REM Next line forces goto end if no match is made 
  838.        GOTO end
  839.        :DOG
  840.        FORMAT A:<yes.txt
  841.        GOTO end
  842.        :CAT
  843.        FORMAT B:<yes.txt
  844.        GOTO end
  845.        :MOUSE
  846.        FORMAT A:<yes.txt
  847.        FORMAT B:<yes.txt
  848.        GOTO end
  849.        :end
  850.        
  851.        Lines 18 and 19 provide formatting of B: immediately after 
  852.        formatting of A: is finished. A short but very powerful batch 
  853.        file. 
  854.        
  855.        Note, how the batch file cleverly uses redirection with the 
  856.        < symbol discussed in our earlier DOS tutorial to send the 
  857.        "stored keystrokes" in the file yes.txt to the format command so 
  858.        you do NOT need to type Yes/No each time the computer formats a 
  859.        disk which is usual when using the format command. Redirection 
  860.        is a very powerful DOS operation. You will also need the file 
  861.        YES.TXT as discussed below. 
  862.  
  863.                              Program name: YES.TXT
  864.  
  865.        This is a very simple file. Start your word processor, DOS Edlin 
  866.        or even use the COPY CONsole command earlier. This tiny file 
  867.        contain a "y" character and two carriage returns and serves as 
  868.        an input file to take the place of the keyboard as explained 
  869.        above. It provides the format command with "simulated" keyboard 
  870.        response of Y (Yes) so the batch file above can continue.
  871.  
  872.        Using Copy Console command (which is one method to prepare
  873.        YES.TXT):
  874.        
  875.        COPY CON YES.TXT     (press enter)
  876.        y                    (press enter)
  877.                             (press enter, skip to new line)
  878.                             (press enter, skip to new line)
  879.        ^Z                   (press F6 to end and write file to disk)
  880.        
  881.                    ---- Make a menu batch file project ----
  882.        
  883.        The next project is actually four simple batch files which work 
  884.        together to provide a "poor man's" menu program for a hard 
  885.        drive. Note the automatic switching between subdirectories 
  886.        provided by the files 1.bat, 2.bat and 3.bat. Obviously you 
  887.        could expand this to include menu choices for more options. Type 
  888.        in each batch file program and save on your hard drive. Place 
  889.        all files in the root directory of your hard drive, usually C:\ 
  890.        When ready to start, simply type M, then press enter key. Notice 
  891.        how M.BAT runs one of the other three files, 1.BAT, 2.BAT or 
  892.        3.BAT. Notice also how when those batch files are done, they 
  893.        automatically run M.BAT to return from where they began and 
  894.        again display the menu screen.
  895.  
  896.                               Program name: M.BAT
  897.        
  898.        REM Third line turns off echo so commands are not repeated to
  899.        REM the screen twice, avoids screen clutter
  900.        ECHO OFF
  901.        REM Next line clears the screen
  902.        CLS
  903.        REM Next line suggests option 1 which will run 1.bat 
  904.        ECHO PRESS 1 FOR WORD PROCESSOR
  905.        REM Next line suggests option 2 which will run 2.bat 
  906.        ECHO PRESS 2 FOR SPREADSHEET
  907.        REM Next line suggests option 3 which will run 3.bat 
  908.        ECHO PRESS 3 FOR DATABASE
  909.        ECHO PRESS ENTER KEY AFTER SELECTION IS MADE
  910.  
  911.                               Program name: 1.BAT
  912.  
  913.        ECHO OFF
  914.        REM Fourth line switches to a subdirectory containing your
  915.        REM word processor using the cd or change directory command
  916.        CD\WP
  917.        REM Seventh line starts your word processor, named word.exe
  918.        REM Substitute the startup command for your word processor
  919.        WORD
  920.        REM Eleventh line changes out of word processing subdirectory 
  921.        REM and goes back to the root directory when word processor
  922.        REM is finished
  923.        CD\
  924.        REM Final line restarts the menu program m.bat
  925.        M
  926.        
  927.                               Program name: 2.BAT
  928.  
  929.        ECHO OFF
  930.        REM Fourth line switches to a subdirectory containing your
  931.        REM spreadsheet using the cd or change directory command
  932.        CD\SPREAD
  933.        REM Seventh line starts your spreadsheet, named lotus.exe
  934.        REM Substitute the start command for your spreadsheet
  935.        LOTUS
  936.        REM Eleventh line changes out of spreadsheet subdirectory
  937.        REM and goes back to the root directory when spreadsheet
  938.        REM is finished
  939.        CD\
  940.        REM Final line restarts the menu program m.bat
  941.        M
  942.        
  943.                               Program name: 3.BAT
  944.        
  945.        ECHO OFF
  946.        REM Fourth line switches to a subdirectory containing your
  947.        REM database using the cd or change directory command
  948.        CD\DATABASE
  949.        REM Seventh line starts your database, named db.exe
  950.        REM Substitute the start command for your database
  951.        DB
  952.        REM Eleventh line changes out of database subdirectory
  953.        REM and goes back to the root directory when database
  954.        REM IS FINISHED
  955.        CD\
  956.        REM Final line restarts the menu program m.bat
  957.        M
  958.        
  959.                       ---- Printer Control Batch file ----
  960.        
  961.        Some of the best kept DOS secrets are fascinating. If you have a 
  962.        standard dot matrix printer connected to your computer, you can 
  963.        use simple batch files to change the typeface and other features 
  964.        your printer provides. The reference booklet which accompanied 
  965.        your printer discusses printer control codes. For example, on 
  966.        Epson compatible printers you will note in your printer book 
  967.        that "emphasized printing" has the DECIMAL control code 27 69. 
  968.        Let's write a batch file to take charge of our printer! 
  969.        
  970.                               Program name: E.BAT
  971.  
  972.        ECHO OFF
  973.        CLS
  974.        ECHO SETTING PRINTER TO EMPHASIZED MODE
  975.        ECHO (ALT 155)(ALT 69) >PRN
  976.        ECHO DONE
  977.  
  978.        The fourth line contains a secret trick. The code we need to 
  979.        send is 27 69 according to our printer book. When preparing this 
  980.        batch file with EDLIN, COPY CONsole or your word processor, you 
  981.        must send the printer control code 27 then 69 to the printer. 
  982.        
  983.        In line four after typing the word "ECHO" then a blank space, 
  984.        hold down the ALT key then press 155 on the FAR RIGHT NUMERIC 
  985.        KEYBOARD. When done entering the number, release the ALT key. 
  986.        On most computers, the cents symbol will appear which the 
  987.        printer will accept as the "escape code 27." DO NOT type the 
  988.        left and right parenthesis marks which appear on line four: ( ), 
  989.        they are only for clarity. 
  990.  
  991.        Next press ALT 69 which produces the E symbol. You could also 
  992.        just type capital E. Next type >PRN which sends this code 
  993.        to your printer. Note that there is NO blank space between (ALT 
  994.        155) and (ALT 69). 
  995.  
  996.        The code 155 is substituted for 27 but the 69 is unchanged. Why 
  997.        155 rather than 27 for the escape code? An explanation: Printer 
  998.        control codes begin with code 128, thus escape character 27 is 
  999.        generated by using 27+128 = 155. An necessary trick for this 
  1000.        batch file. We could send SEVERAL codes by adding more lines to 
  1001.        the batch file to set letter quality, pica font, line spacing 
  1002.        and tabs, then type the letter to the printer (ECHO LETTER.TXT>PRN) 
  1003.        and finally reset the printer. Your printer book discusses these 
  1004.        decimal control codes. 
  1005.  
  1006.        If we wanted to send a formfeed to the printer (eject paper,) 
  1007.        the printer reference book suggests control code 12, therefore 
  1008.        12+128 = 140. So the fourth line in the batch file would read 
  1009.        ECHO (ALT 140) >PRN. The bell sound (decimnal code 7) is 
  1010.        generated by 7+128 = 135. The batch file would change to read 
  1011.        ECHO (ALT 135) >PRN. 
  1012.        
  1013.        Some printer features are controlled by SINGLE control codes 
  1014.        while other features are controlled by MULTIPLE escape sequence 
  1015.        codes which always begin with 27 followed by additional numbers.
  1016.        Escape code 27 is always translated to ALT 155 when DOS and 
  1017.        batch files transmit the printer control information. Within 
  1018.        software programs the codes may use a different format: \027E
  1019.        for example.
  1020.  
  1021.        Many other printing features can be turned on using short batch 
  1022.        files. For example, double strike printing uses the decimal code 
  1023.        27 71. In the batch file you could use ECHO (ALT 155)(ALT 71) >PRN. 
  1024.        
  1025.        You can also turn on a COMBINATION of several features in one 
  1026.        longer batch file to control several features. Just add more 
  1027.        lines and codes to the batch file!
  1028.        
  1029.        To reset the printer when a printing job is finished: code 27 
  1030.        64. Search out these printer codes in your printer manual and 
  1031.        let a batch file do the hard work! 
  1032.  
  1033.        One of the best batch file tutorials to date was published in 
  1034.        two parts in the November and December 1991 editions of PC 
  1035.        Computing Magazine. Contact your library for back issues or 
  1036.        contact PC Computing at Back Issues Dept, PC Computing Magazine, 
  1037.        Ziff Davis, POB 53131, Boulder, CO 80322. Back issues currently 
  1038.        cost $6.00 each.
  1039.                          
  1040.        The bibliography/suggested reading list with PC-Learn provides 
  1041.        additional reading suggestions to advance your batch file 
  1042.        knowledge. 
  1043.  
  1044.        Tutorial finished. Have you registered PC-Learn to receive your
  1045.        bonus disks? Registration is encouraged. Shareware works on the
  1046.        honor system! Send $25 to Seattle Scientific Photography, 
  1047.        Department PCL6, PO Box 1506, Mercer Island, WA 98040. Latest 
  1048.        version of PC-Learn and two bonus disks shipped promptly!
  1049.        
  1050.        
  1051.