home *** CD-ROM | disk | FTP | other *** search
/ Black Box 4 / BlackBox.cdr / dosutils / xdosman.arj / CHAPT7.DOC < prev    next >
Encoding:
Text File  |  1991-11-28  |  41.9 KB  |  1,465 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.                                    Chapter 7.
  10.  
  11.                             The X-DOS Batch Language
  12.  
  13.      Introduction
  14.  
  15.           In addition to the internal and external commands detailed in
  16.           Chapter 6., X-DOS offers a complete batch language. The batch
  17.           language commands can be entered in batch files to allow you to
  18.           write simple programs to carry out repetitive tasks. You can
  19.           create a batch file containing batch commands in ED, the editor
  20.           included with X-DOS and then run this batch file from the command
  21.           line.
  22.  
  23.           Files created in this way are called batch files because they
  24.           contain a batch of commands, and all such files must have an
  25.           extension of .BAT. Each line of a batch file is treated as a
  26.           separate command just as if it had been typed in at the command
  27.           line. The AUTOEXEC.BAT file is an example of a batch file, though
  28.           this is a special file in that X-DOS will always look for this
  29.           file each time you boot up your system.
  30.  
  31.           The commands detailed in this chapter are only for use in batch
  32.           files and cannot be run directly from the command line. Please
  33.           note, however, that the internal and external commands can also
  34.           be entered in batch files.
  35.           With a little practice you will soon be writing batch files to
  36.           prompt for user input, display information on the screen, make
  37.           conditional decisions, call other batch files and much more.
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.                                       7-1
  64.  
  65.  
  66.  
  67.  
  68.  
  69.      The @ Batch Command
  70.  
  71.           Purpose:    This batch command suppresses echoing to the screen
  72.                       of any command it precedes. The output from a command
  73.                       preceded by the @ sign is still displayed on the
  74.                       screen.
  75.  
  76.           Type:       Internal to COMMAND.COM
  77.  
  78.           Syntax:     @[command] [parameters]
  79.  
  80.           Where:      command - is the name of the command you do not want
  81.                       to display on the screen.
  82.  
  83.                       parameters - are any parameters needed by the command
  84.                       you do not want to display on the screen.
  85.  
  86.           Remarks:    Since the ECHO OFF command turns off the display of
  87.                       all succeeding commands in a batch file, the most
  88.                       important use of the @ sign is to ensure that the
  89.                       ECHO OFF command itself does not show on the screen.
  90.                       The @ sign is also useful in situations where you
  91.                       want to display the commands that are executed in the
  92.                       batch file except for maybe one or two commands.
  93.  
  94.           Examples:   If you want to ensure that all commands in your batch
  95.                       file are not displayed on the screen, you normally
  96.                       want to place the ECHO OFF command as the first line
  97.                       of your batch file. To make sure that the ECHO OFF
  98.                       command itself is not displayed on the screen you can
  99.                       precede it with the @ sign, like this:
  100.  
  101.                                     @ECHO OFF
  102.  
  103.                       ECHO OFF is not displayed on the screen when you run
  104.                       the batch file.
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123.  
  124.  
  125.                                       7-2
  126.  
  127.  
  128.  
  129.  
  130.  
  131.      The ANSWER Batch Command
  132.  
  133.           Purpose:    This batch command prompts you for keyboard input.
  134.                       Your input is stored in an environment variable named
  135.                       ANSWER which can later be used by a named
  136.                       substitution variable from within a batch file or
  137.                       from any program.
  138.  
  139.           Type:       Internal to COMMAND.COM
  140.  
  141.           Syntax:     ANSWER [prompt]
  142.  
  143.           Where:      prompt - is an optional text string which ANSWER will
  144.                       display on the screen while waiting for input from
  145.                       the user.
  146.  
  147.           Remarks:    The input you type when ANSWER prompts you is changed
  148.                       to upper-case before it is assigned to the ANSWER
  149.                       variable in the environment. You can therefore not
  150.                       make case dependent tests on ANSWER input.
  151.  
  152.                       If the optional prompt is not specified, ANSWER will
  153.                       not display anything on the screen that will inform
  154.                       the user that input is expected. Use the ECHO or
  155.                       REVECHO commands prior to the ANSWER command or use
  156.                       the optional prompt with ANSWER to make the user
  157.                       aware that keyboard input is expected.
  158.  
  159.                       ANSWER returns an error code which can be examined by
  160.                       the ERRORLEVEL option of the IF command. The
  161.                       errorlevel will be set to 0 if the ANSWER variable is
  162.                       successfully stored in the environment. If there is
  163.                       insufficient room in the environment to store the
  164.                       ANSWER variable or if the environment area is
  165.                       corrupted, ANSWER returns an error code of 1.
  166.  
  167.                       You should not use the >, < or | characters in the
  168.                       ANSWER prompt since they would be interpreted as
  169.                       redirection symbols.
  170.  
  171.           Examples:   In the following example we assume that you have
  172.                       created a batch file in which you want to prompt the
  173.                       user for a password before continuing execution of
  174.                       the batch file. To do this you should include the
  175.                       following line in your batch file:
  176.  
  177.                           ANSWER Enter Your password :
  178.  
  179.                       If the user types "SECRET" as his password, the
  180.                       ANSWER variable is set equal to SECRET in the
  181.                       environment. To verify this try to make a batch file
  182.                       which only contains the above line and execute it.
  183.  
  184.  
  185.  
  186.  
  187.                                       7-3
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194.                       Then type the password SECRET. When the batch file is
  195.                       finished executing you can execute the SET command,
  196.                       like this:
  197.  
  198.                                       SET
  199.  
  200.                       You will see a listing of all current environment
  201.                       variables. Among them you should see one saying:
  202.  
  203.                                  ANSWER=SECRET
  204.  
  205.                       You can then later use a named environment variable
  206.                       to use or check the setting of the ANSWER environment
  207.                       variable. A real batch file in which ANSWER is used
  208.                       could look similar to this:
  209.  
  210.                         ANSWER Please enter your name :
  211.  
  212.                             IF %ANSWER%&==& GOTO TOP
  213.  
  214.                     ECHO Hello %ANSWER%, how are you today?
  215.  
  216.                       You may be wondering about the purpose of the fourth
  217.                       line of the batch file, IF %ANSWER%&==& GOTO TOP. It
  218.                       simply checks to see that you have entered any text.
  219.                       If the ANSWER environment variable is empty, which is
  220.                       the case if you only press [Enter], the line will be
  221.                       interpreted by X-DOS like this:
  222.  
  223.                                 IF &==& GOTO TOP
  224.  
  225.                       The & character is only used to ensure that both
  226.                       sides of the == signs contain information. You do not
  227.                       have to use the & character.You can actually use any
  228.                       character just as long as it is the same character on
  229.                       both sides of the == signs. If the above IF test is
  230.                       TRUE (&==&) then you know that the ANSWER environment
  231.                       variable is empty and you can take a proper action.
  232.                       In the above example we simply go back and prompt the
  233.                       user to enter his or her name again.
  234.  
  235.  
  236.  
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249.                                       7-4
  250.  
  251.  
  252.  
  253.  
  254.  
  255.      The BEEP Batch Command
  256.  
  257.           Purpose:    This batch command produces a beep on the computer's
  258.                       speaker which can be used to get the user's attention
  259.                       during batch file execution.
  260.  
  261.           Type:       Internal to COMMAND.COM
  262.  
  263.           Syntax:     BEEP
  264.  
  265.           Remarks:    This batch command is useful in situations where you
  266.                       want to make the user aware that input is needed or
  267.                       an error has occurred.
  268.  
  269.  
  270.  
  271.  
  272.  
  273.  
  274.  
  275.  
  276.  
  277.  
  278.  
  279.  
  280.  
  281.  
  282.  
  283.  
  284.  
  285.  
  286.  
  287.  
  288.  
  289.  
  290.  
  291.  
  292.  
  293.  
  294.  
  295.  
  296.  
  297.  
  298.  
  299.  
  300.  
  301.  
  302.  
  303.  
  304.  
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311.                                       7-5
  312.  
  313.  
  314.  
  315.  
  316.  
  317.      The CALL Batch Command
  318.  
  319.           Purpose:    This batch command allows one batch file to call
  320.                       another batch file, execute this batch file, and then
  321.                       return to the calling batch file and resume execution
  322.                       right after the CALL command.
  323.  
  324.           Type:       Internal to COMMAND.COM
  325.  
  326.           Syntax:     CALL [d:][path]filename
  327.  
  328.           Where:      d:path - is the drive and path to the batch file you
  329.                       want to call.
  330.  
  331.                       filename - is the name of the batch file you want to
  332.                       call.
  333.  
  334.           Remarks:    The CALL command gives you great flexibility in
  335.                       setting up modular batch files. This is especially
  336.                       useful if you are making batch files which will be
  337.                       executed by different users.
  338.  
  339.                       If you do not specify a command from which X-DOS
  340.                       should continue executing after it returns to the
  341.                       calling batch file, X-DOS automatically starts
  342.                       execution from the command following the CALL
  343.                       command. You can have up to 10 nested CALL commands.
  344.                       This, in other words, means that you can have one
  345.                       batch file call another batch file which calls
  346.                       another batch file, which calls another batch file,
  347.                       up to 10 nested calls. You can also make recursive
  348.                       calls which means that a batch file calls itself.
  349.                       Just remember to ensure that you do not set the batch
  350.                       file up to run in an endless loop, which can easily
  351.                       happen if you do not insert some kind of termination
  352.                       condition.
  353.  
  354.           Examples:   We assume that you insert the command:
  355.  
  356.                              CALL C:\BATCH\JOHN.BAT
  357.  
  358.                       in a batch file. When you execute the batch file, and
  359.                       X-DOS reaches the CALL command, it will abort the
  360.                       execution of the current batch file and hand over
  361.                       control to the JOHN.BAT batch file in the C:\BATCH
  362.                       directory. When this batch file terminates, X-DOS
  363.                       will return to the calling batch file and continue
  364.                       execution right after the CALL C:\BATCH\JOHN.BAT
  365.                       command.
  366.  
  367.  
  368.  
  369.  
  370.  
  371.  
  372.  
  373.                                       7-6
  374.  
  375.  
  376.  
  377.  
  378.  
  379.      The CHECK Batch Command
  380.  
  381.           Purpose:    This batch command enhances the batch language by
  382.                       reporting a variety of parameters that can range from
  383.                       disk information to time and date functions. Any
  384.                       results that the CHECK command returns can be
  385.                       evaluated with the ERRORLEVEL batch sub-command.
  386.  
  387.           Type:       Internal to COMMAND.COM
  388.  
  389.           Syntax:     CHECK keyword [parameters]
  390.  
  391.           Where:      keyword - is one of the keywords which are recognized
  392.                       by the CHECK command. Each of these keywords are
  393.                       described below.
  394.  
  395.                       parameters - are any parameters which may be required
  396.                       by the specified keyword.
  397.  
  398.           Remarks:    CHECK should come in very handy in automating many of
  399.                       the mundane tasks you perform. As an example, maybe
  400.                       you go through and delete all of your *.BAK files the
  401.                       beginning of every month. You can identify the months
  402.                       changing by including the CHECK day command in your
  403.                       batch file. If the day is equal to one (1), it is a
  404.                       good indication that the month has changed. You can
  405.                       then branch to your sub-routine for deleting the
  406.                       *.BAK files.
  407.  
  408.           Disk Related Keywords
  409.           DISKSPACE returns the amount of free disk space on the specified
  410.           drive, or on the default drive in terms of whole 16K byte blocks.
  411.           If there is 120K of free disk space, DISKSPACE returns the value
  412.           of 8, which can be used with ERRORLEVEL to indicate 8 blocks are
  413.           free.
  414.  
  415.           FILESIZE reports the length of a given file in kilobytes. A value
  416.           of 255 means that the file length is 255K or greater in
  417.           size.FILEFOUND takes the place of other DOSs IF EXIST conditional
  418.           testing and returns a 0 if a file exists and a 1 if it does not
  419.           exist.
  420.  
  421.           FILETEXT searches for a text string inside a file. See the
  422.           example below:
  423.  
  424.                 CHECK filetext C:\DOS\NOTES.MSG `Things to do:'
  425.  
  426.           In the above example, NOTES.MSG is checked for on Drive C: in the
  427.           sub-directory \DOS and if present, the string Things to do: is
  428.           searched for. If the string is found, a 0 is returned; if not, or
  429.           if an error is encountered, such as the file is not on the
  430.           specified path, a 1 is returned. You must enclose the string to
  431.  
  432.  
  433.  
  434.  
  435.                                       7-7
  436.  
  437.  
  438.  
  439.  
  440.  
  441.           be searched for in single quotes (`').
  442.  
  443.           Hardware Related Keywords
  444.           MEMORY - number of 16K memory blocks.
  445.  
  446.           VIDEOTYPE - Monochrome Display Adapter (MGA or Hercules). Color
  447.           Graphics Adapter (CGA). Enhanced Graphics Adapter (EGA or VGA).
  448.  
  449.           MATHCO - Math co-processor installed. Math co-processor not
  450.           installed.
  451.  
  452.           These options check the hardware configuration. You may have a
  453.           program that you keep in two different versions, (i.e. 43 line
  454.           format for EGA, and 25 line format for monochrome) and you wish
  455.           to know what video card is installed so that you can call the
  456.           appropriate configuration. Or, you may have a program installed
  457.           for both, with co-processor and without co-processor.
  458.           CHECK will identify these things for you and report to your batch
  459.           file what it has found.
  460.  
  461.           Miscellaneous Keywords
  462.           TIME - returns the current time.
  463.  
  464.           DAY - returns the current day.
  465.  
  466.           MONTH - returns the current month.
  467.  
  468.           KEYPRESS  - returns the ASCII code of any key pressed.
  469.           Using those keys that produce an extended code like the function
  470.           keys and the arrow keys will set the ERRORLEVEL. These keys in
  471.           conjunction with X-DOS's KEYIN command, listed elsewhere in this
  472.           section, enhance the ability of the batch language to interact
  473.           with the user.
  474.  
  475.           Examples:   Here are some examples of the options of CHECK, in
  476.                       everyday use. The following batch file will delete
  477.                       all .BAK files if free disk space is less than 64K.
  478.  
  479.                CHECK DISKSPACE A:IF NOT ERRORLEVEL 4 DEL A:*.BAK
  480.  
  481.                       This batch file will delete a file if it exceeds 16K
  482.                       in size.
  483.  
  484.                    CHECK FILESIZE %1 IF ERRORLEVEL 17 DEL %1
  485.  
  486.                       This batch file decides whether to delete a file on
  487.                       the default drive, based on whether it is present on
  488.                       another specified drive.
  489.  
  490.         CHECK FILEFOUND B:EXAMPLE.FLE IF ERRORLEVEL 1 DEL C:EXAMPLE.FLE
  491.  
  492.                       The following batch file chooses which configuration
  493.  
  494.  
  495.  
  496.  
  497.                                       7-8
  498.  
  499.  
  500.  
  501.  
  502.  
  503.                       of an installed program to use, based on the video
  504.                       card that is installed.
  505.  
  506.             CHECK VIDEOTYPE IF ERRORLEVEL 1 GOTO COLORVIDEOWS1GOTO
  507.  
  508.                              END:COLORVIDEOWS2:END
  509.  
  510.  
  511.  
  512.  
  513.  
  514.  
  515.  
  516.  
  517.  
  518.  
  519.  
  520.  
  521.  
  522.  
  523.  
  524.  
  525.  
  526.  
  527.  
  528.  
  529.  
  530.  
  531.  
  532.  
  533.  
  534.  
  535.  
  536.  
  537.  
  538.  
  539.  
  540.  
  541.  
  542.  
  543.  
  544.  
  545.  
  546.  
  547.  
  548.  
  549.  
  550.  
  551.  
  552.  
  553.  
  554.  
  555.  
  556.  
  557.  
  558.  
  559.                                       7-9
  560.  
  561.  
  562.  
  563.  
  564.  
  565.      The DELAY Batch Command
  566.  
  567.           Purpose:    This batch command lets you delay batch file
  568.                       processing for a specified amount of time.
  569.  
  570.           Type:       Internal to COMMAND.COM
  571.  
  572.           Syntax:     DELAY xx
  573.  
  574.           Where:      xx - are the number of seconds to delay batch file
  575.                       processing.
  576.  
  577.           Remarks:    The DELAY command is handy when you want to display
  578.                       important messages that the user should read before
  579.                       the batch file automatically continues execution.
  580.  
  581.           Examples:   If you want to display the message:
  582.  
  583.                       Remember to turn off your computer!
  584.  
  585.                       and then delay batch file execution for 3 seconds to
  586.                       allow the user enough time to read the message,
  587.                       include the following two lines in a batch file:
  588.  
  589.                 ECHO Remember to turn off you computer! DELAY 3
  590.  
  591.  
  592.  
  593.  
  594.  
  595.  
  596.  
  597.  
  598.  
  599.  
  600.  
  601.  
  602.  
  603.  
  604.  
  605.  
  606.  
  607.  
  608.  
  609.  
  610.  
  611.  
  612.  
  613.  
  614.  
  615.  
  616.  
  617.  
  618.  
  619.  
  620.  
  621.                                       7-10
  622.  
  623.  
  624.  
  625.  
  626.  
  627.      The ECHO Batch Command
  628.  
  629.           Purpose:    This batch command is used to display a message on
  630.                       the screen or to suppress output to the screen.
  631.  
  632.           Type:       Internal to COMMAND.COM
  633.  
  634.           Syntax:     ECHO [ON | OFF]
  635.                       or
  636.                       ECHO [message]
  637.  
  638.           Where:      ON - turns ON the display of the commands in a batch
  639.                       file so that they are displayed on the screen as they
  640.                       are being executed. ECHO ON is the default setting.
  641.  
  642.                       OFF - turns OFF the display of the commands in a
  643.                       batch file. This way only the output from the
  644.                       commands that X-DOS executes in the batch file is
  645.                       displayed.
  646.  
  647.                       message - displays a message on the screen. This
  648.                       message can be up to 127 characters long.
  649.  
  650.           Remarks:    The default setting of ECHO ON is very practical when
  651.                       you are developing a batch file and want to be able
  652.                       to see which commands are being executed from the
  653.                       batch file for debugging purposes. Once a batch file
  654.                       is running flawlessly, there is no longer any reason
  655.                       to display the commands that are executed from the
  656.                       batch file. To suppress the display of the commands,
  657.                       turn the ECHO mode OFF at the beginning of the batch
  658.                       file.
  659.  
  660.                       When the ECHO mode is turned OFF, you can use the
  661.                       ECHO command to display a message on the screen. The
  662.                       REVECHO command also displays a message on the screen
  663.                       in a similar manner, but in reverse video. If you
  664.                       expect the user to type in an answer to your message
  665.                       you should use the ANSWER command to display the
  666.                       message.
  667.  
  668.                       If you execute ECHO without any parameters, it will
  669.                       display the current setting of the ECHO mode, which
  670.                       is either ON or OFF. If you have turned ECHO mode ON,
  671.                       you can disable echoing of individual commands by
  672.                       putting the `@' symbol in front of the command.
  673.  
  674.           Examples:   If you want to suppress the display of the commands
  675.                       that are executed in a batch file, you need to turn
  676.                       the ECHO mode OFF. To do that you will need to the
  677.                       following ECHO command at the top of the batch file:
  678.  
  679.  
  680.  
  681.  
  682.  
  683.                                       7-11
  684.  
  685.  
  686.  
  687.  
  688.  
  689.                                     ECHO OFF
  690.  
  691.                       You can view the status of the current ECHO mode by
  692.                       typing:
  693.  
  694.                                       ECHO
  695.  
  696.  
  697.  
  698.  
  699.  
  700.  
  701.  
  702.  
  703.  
  704.  
  705.  
  706.  
  707.  
  708.  
  709.  
  710.  
  711.  
  712.  
  713.  
  714.  
  715.  
  716.  
  717.  
  718.  
  719.  
  720.  
  721.  
  722.  
  723.  
  724.  
  725.  
  726.  
  727.  
  728.  
  729.  
  730.  
  731.  
  732.  
  733.  
  734.  
  735.  
  736.  
  737.  
  738.  
  739.  
  740.  
  741.  
  742.  
  743.  
  744.  
  745.                                       7-12
  746.  
  747.  
  748.  
  749.  
  750.  
  751.      The FOR Batch Command
  752.  
  753.           Purpose:    This batch command is used in combination with the IN
  754.                       and DO batch sub-commands to repeat execution of a X-
  755.                       DOS command or another program.
  756.  
  757.           Type:       Internal to COMMAND.COM
  758.  
  759.           Syntax:     FOR %%V IN (params) DO command %%V
  760.  
  761.           Where:      V - is a one-character substitution variable which
  762.                       can be other characters than the character 'V' used
  763.                       here. This variable cannot be a digit (0 to 9) in
  764.                       order to avoid confusion with the other substitution
  765.                       variables that can be used in the params section of
  766.                       the FOR command.
  767.  
  768.                       params - is a list of parameters which in turn are
  769.                       passed to the %%V substitution variable. These
  770.                       parameters can be any mixture of text strings,
  771.                       substitution variables, and named substitution
  772.                       variables.
  773.  
  774.                       command - is the name of the command you want to
  775.                       execute including any necessary parameters that the
  776.                       command needs.
  777.  
  778.           Remarks:    When the FOR command executes it will take each
  779.                       filespec in params and set it in place of the %%V
  780.                       substitution variable. If a filespec includes any
  781.                       wildcards, each filename matching the filespec
  782.                       replaces the %%V variable one by one.
  783.  
  784.                       You will notice that the substitution variable %%V
  785.                       has two percentage signs (%%) in front of it instead
  786.                       only one percentage sign (%). The reason for this is
  787.                       that you want X-DOS to assign values to the
  788.                       substitution variables of filespecs the first time
  789.                       around and then assign the values of the substitution
  790.                       variables of filespecs to the %%V substitution
  791.                       variable the second time around. Each time X-DOS
  792.                       assigns values to substitution variables it removes
  793.                       one percentage sign.
  794.  
  795.           Examples:   If you want to create a batch file that can delete
  796.                       multiple filespecs by issuing only one command, you
  797.                       can include a FOR command like this:
  798.                       FOR %%M IN (%1 %2 %3 %4) DO DEL %%M
  799.  
  800.  
  801.  
  802.  
  803.  
  804.  
  805.  
  806.  
  807.                                       7-13
  808.  
  809.  
  810.  
  811.  
  812.  
  813.      The GOTO Batch Command
  814.  
  815.           Purpose:    This batch command continues to execute the batch
  816.                       file a different place as specified by a label name.
  817.  
  818.           Type:       Internal to COMMAND.COM
  819.  
  820.           Syntax:     GOTO [:]labelname
  821.  
  822.           Where:      labelname - is a reference to a label some place in
  823.                       the batch file. You must specify a colon (:) in front
  824.                       of a label name in a batch file.
  825.  
  826.           Remarks:    The GOTO command instructs X-DOS to jump to a
  827.                       specific place in a batch file which has the
  828.                       specified label name assigned to it. A label is any
  829.                       combination of up to eight alphanumeric characters
  830.                       and must always be preceded by a colon (:).
  831.                       References to labels do not need to be preceded by a
  832.                       colon (:).
  833.  
  834.                       The GOTO command is normally used in combination with
  835.                       the ERRORLEVEL command to branch to another place in
  836.                       the batch file if a condition is true or not true.
  837.  
  838.           Examples:   The following example batch file shows how you can
  839.                       use the GOTO command:
  840.                  :STARTIF ERRORLEVEL 5 GOTO EXITECHO HELLOEXIT:
  841.  
  842.  
  843.  
  844.  
  845.  
  846.  
  847.  
  848.  
  849.  
  850.  
  851.  
  852.  
  853.  
  854.  
  855.  
  856.  
  857.  
  858.  
  859.  
  860.  
  861.  
  862.  
  863.  
  864.  
  865.  
  866.  
  867.  
  868.  
  869.                                       7-14
  870.  
  871.  
  872.  
  873.  
  874.  
  875.      The IF Batch Command
  876.  
  877.           Purpose:    This batch command is used with the EXIST,
  878.                       ERRORLEVEL, and NOT sub-commands to execute or skip
  879.                       execution of a command based on a conditional
  880.                       decision.
  881.  
  882.           Type:       Internal to COMMAND.COM
  883.  
  884.           Syntax:     IF [NOT] condition command
  885.  
  886.           Where:      NOT - means that the command will be executed if the
  887.                       condition is FALSE.
  888.  
  889.                       condition - is one of three possible expressions:
  890.  
  891.                       ERRORLEVEL number - makes the condition TRUE if the
  892.                       program previously executed by COMMAND.COM returned
  893.                       an exit code equal to , or greater than, number.
  894.  
  895.                       string1==string2 - makes the condition TRUE if
  896.                       string1 equals string2 after a substitution of
  897.                       substitution variables have occurred. For this
  898.                       condition to make any sense, one or both of the text
  899.                       strings have to be a substitution variable.
  900.  
  901.                       EXIST [d:][path]filespec - makes the condition TRUE
  902.                       if a file or range of files exist. You can specify an
  903.                       optional drive and path to where you want IF to check
  904.                       for the file(s).
  905.  
  906.                       command - is any X-DOS command or other program which
  907.                       will be executed if the condition is TRUE, or, if the
  908.                       NOT parameter is specified, is FALSE.
  909.  
  910.           Remarks:    Any of the three conditions may be preceded by the
  911.                       NOT command to indicate that the following command
  912.                       should be executed only if the condition is not True.
  913.  
  914.                       Special care must be taken when comparing two strings
  915.                       of the form string1==string2 of which one or both of
  916.                       the strings are named substitution variables. You
  917.                       must ensure that variables, referenced by string1
  918.                       and/or string2, have been set up in the environment.
  919.                       Otherwise, substitution variables without a reference
  920.                       in the environment are literally going to be
  921.                       substituted with nothing. Look at the following
  922.                       example:
  923.  
  924.               IF NOT %TEST%==BAD ECHO Test completed successfully.
  925.  
  926.                       If an environment variable named TEST is set to any
  927.  
  928.  
  929.  
  930.  
  931.                                       7-15
  932.  
  933.  
  934.  
  935.  
  936.  
  937.                       value, the batch file is going to perform as
  938.                       expected. The ECHO statement is going to be executed
  939.                       in all instances except when TEST is set to `BAD'.
  940.                       But, if no variable named TEST is found in the
  941.                       environment, X-DOS interprets the above command this
  942.                       way:
  943.               IF NOT %TEST%==BAD ECHO Test completed successfully.
  944.  
  945.                       Since on TEST variable was found in the environment,
  946.                       X-DOS substituted %TEST% with absolutely nothing.
  947.                       When the test is performed, string1 is missing and
  948.                       you will receive a syntax error. You can avoid this
  949.                       situation by testing for the existence of the TEST
  950.                       variable in the environment. A way of doing this is
  951.                       to make a test similar to this:
  952.  
  953.                            IF %TEST%&==& GOTO NO_VAR
  954.  
  955.                       By including the '&' character on both sides of the
  956.                       '==' signs, you ensure that the left side at least
  957.                       will consist of the '&' character. If this test is
  958.                       true (&==&) then you know that the environment
  959.                       variable TEST is missing and you should make the
  960.                       batch file jump to a place where you give the proper
  961.                       error message.
  962.  
  963.           Examples:   If you want to create a batch file which you want to
  964.                       use in place of the DEL command to ensure that you do
  965.                       not accidentally delete files with an extension of
  966.                       .DOC by using the *.DOC filespec. This file, which we
  967.                       call NDEL.BAT, could look like this:
  968.  
  969.       IF %1== *.DOC GOTO WARNINGDEL %1 GOTO EXIT:WARNING ECHO You cannot
  970.  
  971.                   delete files with the *.DOC filespec!!!:EXIT
  972.  
  973.  
  974.  
  975.  
  976.  
  977.  
  978.  
  979.  
  980.  
  981.  
  982.  
  983.  
  984.  
  985.  
  986.  
  987.  
  988.  
  989.  
  990.  
  991.  
  992.  
  993.                                       7-16
  994.  
  995.  
  996.  
  997.  
  998.  
  999.      The KEYIN Batch Command
  1000.  
  1001.           Purpose:    This batch command enables you to create interactive
  1002.                       batch files which accept only a predefined set of
  1003.                       keys. This command is especially useful when making
  1004.                       batch file menu systems.
  1005.  
  1006.           Type:       Internal to COMMAND.COM
  1007.  
  1008.           Syntax:     KEYIN A [B C 1 (...)]
  1009.  
  1010.           Where:      A - is any one ASCII character that KEYIN will use
  1011.                       accept before continuing execution of the batch file.
  1012.  
  1013.                       B C 1 - are additional ASCII characters that KEYIN
  1014.                       will accept before continuing execution of the batch
  1015.                       file.
  1016.  
  1017.                       (...) - shows that KEYIN accepts several more ASCII
  1018.                       characters as parameters. You can specify as many
  1019.                       parameters as will fit on the command line and you do
  1020.                       not have to separate the parameters with spaces.
  1021.  
  1022.           Remarks:    The ASCII characters that you use as parameters for
  1023.                       the KEYIN command do not have to be separated by
  1024.                       spaces and each KEYIN parameter can only consist of
  1025.                       one character. KEYIN is not case sensitive and
  1026.                       therefore treats upper- and lower-case letters the
  1027.                       same.
  1028.  
  1029.                       The KEYIN command uses IF ERRORLEVEL testing to check
  1030.                       the input from the user and screen out illegal
  1031.                       entries, meaning any ASCII characters which are not
  1032.                       specified as KEYIN parameters. Any illegal entries
  1033.                       are automatically assigned the exit code 0 and can
  1034.                       therefore be dealt with equally by the author of the
  1035.                       batch file. The first KEYIN parameter is assigned the
  1036.                       exit code 1, the second KEYIN parameter is assigned
  1037.                       the exit code 2, and so forth.
  1038.  
  1039.                       Therefore, when you use IF ERRORLEVEL testing to
  1040.                       check the user's input, you must always put the IF
  1041.                       ERRORLEVEL testing with the highest exit code on top
  1042.                       of the IF ERRORLEVEL testing list since the
  1043.                       ERRORLEVEL command always check for exit codes equal
  1044.                       to or higher than the specified ERRORLEVEL number.
  1045.  
  1046.           Examples:   Suppose the following BATCH file :
  1047.                                      :START
  1048.  
  1049.                             ECHO 1. Word Processing
  1050.  
  1051.  
  1052.  
  1053.  
  1054.  
  1055.                                       7-17
  1056.  
  1057.  
  1058.  
  1059.  
  1060.  
  1061.                               ECHO 2. Spreadsheet
  1062.  
  1063.                                 ECHO 3. Database
  1064.  
  1065.                                   ECHO 4. Exit
  1066.  
  1067.                              ECHO INPUT YOUR CHOICE
  1068.  
  1069.                       KEYIN 1234IF ERRORLEVEL 4 GOTO EXIT
  1070.  
  1071.                            IF ERRORLEVEL 3 GOTO DATAB
  1072.  
  1073.                           IF ERRORLEVEL 2 GOTO SPREAD
  1074.  
  1075.                             IF ERRORLEVEL 1 GOTO WP
  1076.  
  1077.                                       : WP
  1078.  
  1079.                                       ....
  1080.  
  1081.                                     : SPREAD
  1082.  
  1083.                                       ....
  1084.  
  1085.                                     : DATAB
  1086.  
  1087.                                       ....
  1088.  
  1089.                                      : EXIT
  1090.  
  1091.  
  1092.  
  1093.  
  1094.  
  1095.  
  1096.  
  1097.  
  1098.  
  1099.  
  1100.  
  1101.  
  1102.  
  1103.  
  1104.  
  1105.  
  1106.  
  1107.  
  1108.  
  1109.  
  1110.  
  1111.  
  1112.  
  1113.  
  1114.  
  1115.  
  1116.  
  1117.                                       7-18
  1118.  
  1119.  
  1120.  
  1121.  
  1122.  
  1123.      The LOCATE Batch Command
  1124.  
  1125.           Purpose:    This batch command places the cursor at a specified
  1126.                       location on your screen to allow you to display text
  1127.                       anywhere on the screen.
  1128.  
  1129.           Type:       Internal to COMMAND.COM
  1130.  
  1131.           Syntax:     LOCATE [R][,C]
  1132.  
  1133.           Where:      R - is the screen row in decimal numbers. The upper-
  1134.                       left corner of the screen is row 0 and the screen has
  1135.                       25 or 43 rows.
  1136.  
  1137.                       C - is the screen column in decimal numbers. The
  1138.                       upper-left corner of the screen is column 0 and the
  1139.                       screen has 80 columns.
  1140.  
  1141.           Remarks:    You can use LOCATE with only the Row (R) parameter in
  1142.                       which case the column defaults to column 0. It is
  1143.                       also possible to use the LOCATE command with only the
  1144.                       Column (C) parameter. Just precede the column
  1145.                       parameter with a comma.
  1146.  
  1147.  
  1148.  
  1149.  
  1150.  
  1151.  
  1152.  
  1153.  
  1154.  
  1155.  
  1156.  
  1157.  
  1158.  
  1159.  
  1160.  
  1161.  
  1162.  
  1163.  
  1164.  
  1165.  
  1166.  
  1167.  
  1168.  
  1169.  
  1170.  
  1171.  
  1172.  
  1173.  
  1174.  
  1175.  
  1176.  
  1177.  
  1178.  
  1179.                                       7-19
  1180.  
  1181.  
  1182.  
  1183.  
  1184.  
  1185.      The LOOP Batch Command
  1186.  
  1187.           Purpose:    This batch command can be used to loop part of a
  1188.                       batch file a number of times.
  1189.  
  1190.           Type:       Internal to COMMAND.COM
  1191.  
  1192.           Syntax:     LOOP /IlabelnameLOOP..IF [NOT] ERRORLEVEL number GOTO
  1193.                       labelname
  1194.  
  1195.           Where:      I - sets the loop counter to 0. You should always
  1196.                       initialize the LOOP command before beginning the
  1197.                       actual looping to ensure that it starts counting from
  1198.                       0.
  1199.  
  1200.           Remarks:    The initial LOOP statement with the /I parameter
  1201.                       resets the counter for LOOP. You should always
  1202.                       initialize the LOOP command before beginning the
  1203.                       actual looping. The second LOOP statement actually
  1204.                       performs the loop until a condition is met according
  1205.                       to the ERRORLEVEL checking.
  1206.  
  1207.                       Every time the LOOP command is executed without any
  1208.                       parameters it increases the exit code by one. In your
  1209.                       IF ERRORLEVEL testing you just specify a value which
  1210.                       reflects the number of times you want the loop to be
  1211.                       executed.
  1212.  
  1213.           Please be aware that IF ERRORLEVEL testing does not support exit
  1214.           codes above 255, which then is the maximum number of loops you
  1215.           can perform with one LOOP command.
  1216.  
  1217.  
  1218.  
  1219.  
  1220.  
  1221.  
  1222.  
  1223.  
  1224.  
  1225.  
  1226.  
  1227.  
  1228.  
  1229.  
  1230.  
  1231.  
  1232.  
  1233.  
  1234.  
  1235.  
  1236.  
  1237.  
  1238.  
  1239.  
  1240.  
  1241.                                       7-20
  1242.  
  1243.  
  1244.  
  1245.  
  1246.  
  1247.      The PAUSE Batch Command
  1248.  
  1249.           Purpose:    This batch command stops batch file execution until
  1250.                       the user presses a key. You can optionally specify a
  1251.                       message which will be displayed on the screen when
  1252.                       the PAUSE command is executed.
  1253.  
  1254.           Type:       Internal to COMMAND.COM
  1255.  
  1256.           Syntax:     PAUSE [message]
  1257.  
  1258.           Where:      message - is the message to be displayed while the
  1259.                       PAUSE command waits from input from the user.
  1260.  
  1261.           Remarks:    If no message is entered after PAUSE, the PAUSE
  1262.                       command will display the default message "Press any
  1263.                       key to continue". The user must then press any key at
  1264.                       this point to continue the execution of the batch
  1265.                       file. If you need the user to enter input which is
  1266.                       going to be used later in the batch file you should
  1267.                       use the ANSWER command instead of the PAUSE command.
  1268.  
  1269.           Examples:   If you want to display the message "You have now
  1270.                       completed the installation" on the screen from a
  1271.                       batch file, you can do so by including the following
  1272.                       PAUSE statement:
  1273.  
  1274.                  PAUSE You have now completed the installation
  1275.  
  1276.  
  1277.  
  1278.  
  1279.  
  1280.  
  1281.  
  1282.  
  1283.  
  1284.  
  1285.  
  1286.  
  1287.  
  1288.  
  1289.  
  1290.  
  1291.  
  1292.  
  1293.  
  1294.  
  1295.  
  1296.  
  1297.  
  1298.  
  1299.  
  1300.  
  1301.  
  1302.  
  1303.                                       7-21
  1304.  
  1305.  
  1306.  
  1307.  
  1308.  
  1309.      The REM Batch and Configuration Command
  1310.  
  1311.           Purpose:    Inserts comments or remarks in a batch file
  1312.  
  1313.           Type:       Internal to COMMAND.COM
  1314.  
  1315.           Syntax:     REM [text]
  1316.  
  1317.           Where:      text - is any string of character up to 127 bytes
  1318.                       long
  1319.  
  1320.           Remarks:    When X-DOS encounters a REM command in a batch file
  1321.                       it automatically skips the command and jumps to the
  1322.                       next line in the batch file.You can also use the
  1323.                       colon (:) to insert remarks in your batch file. The
  1324.                       colon is used in batch files to distinguish labels
  1325.                       which are just treated as normal text by X-DOS. Just
  1326.                       ensure that the first word of the remark is not the
  1327.                       name of a valid label that you are already using
  1328.                       elsewhere in the batch file.
  1329.  
  1330.           Examples:   You want to create a batch file which copies multiple
  1331.                       filespecs onto the diskette in the A: drive and you
  1332.                       want to include a remark at the top of the batch file
  1333.                       which explains the purpose of the batch file. Such a
  1334.                       remark could look like this:
  1335.  
  1336.      REM This batch file copies several filespecs onto a diskette in the A:
  1337.  
  1338.                                      drive
  1339.  
  1340.  
  1341.  
  1342.       REM You must specify the filespecs as parameters to this batch file.
  1343.  
  1344.  
  1345.  
  1346.  
  1347.  
  1348.  
  1349.  
  1350.  
  1351.  
  1352.  
  1353.  
  1354.  
  1355.  
  1356.  
  1357.  
  1358.  
  1359.  
  1360.  
  1361.  
  1362.  
  1363.  
  1364.  
  1365.                                       7-22
  1366.  
  1367.  
  1368.  
  1369.  
  1370.  
  1371.      The REVECHO Batch Command
  1372.  
  1373.           Purpose:    This command is used to display text on the screen in
  1374.                       reverse text.
  1375.  
  1376.           Type:       Internal to COMMAND.COM
  1377.  
  1378.           Syntax:     REVECHO [text]
  1379.  
  1380.           Where:      text - is any string of character up to 127 bytes
  1381.                       long
  1382.  
  1383.           Remarks:    The X-DOS ECHO command provides a simple way to
  1384.                       display messages from within batch files. REVECHO
  1385.                       works the same way, except that it displays the
  1386.                       message in reverse video.
  1387.  
  1388.                       By using REVECHO in combination with the LOCATE
  1389.                       command and the LOOP command, you can make a message
  1390.                       flash on the screen. Since REVECHO looks at the
  1391.                       existing color and reverses it each subsequent time
  1392.                       you execute it, the color will appear to flash if
  1393.                       executed many times in succession.
  1394.  
  1395.           Examples:   A batch file which would flash the message "Warning!"
  1396.                       in the middle of the screen could look similar to
  1397.                       this:
  1398.  
  1399.                                       CLS
  1400.  
  1401.                                     LOOP /I
  1402.  
  1403.                                    :FLASHMES
  1404.  
  1405.                                LOOP>LOCATE 12,35
  1406.  
  1407.                                 REVECHO Warning!
  1408.  
  1409.                       IF NOT ERRORLEVEL 100 GOTO FLASHMES
  1410.  
  1411.  
  1412.  
  1413.  
  1414.  
  1415.  
  1416.  
  1417.  
  1418.  
  1419.  
  1420.  
  1421.  
  1422.  
  1423.  
  1424.  
  1425.  
  1426.  
  1427.                                       7-23
  1428.  
  1429.  
  1430.  
  1431.  
  1432.  
  1433.      The SHIFT Batch Command
  1434.  
  1435.           Purpose:    The shift command makes it possible for you to use
  1436.                       more than the standard ten substitution variables, %0
  1437.                       to %9.
  1438.  
  1439.           Type:       Internal to COMMAND.COM
  1440.  
  1441.           Syntax:     SHIFT
  1442.  
  1443.           Remarks:    SHIFT will let you move the value of a substitution
  1444.                       variable down one step at a time (e.g., from %4 to %3
  1445.                       to %2 etc.). When you do this, you will lose the
  1446.                       value of the lowest substitution variable, %0. If you
  1447.                       need to retain a lower value, you can use the SET
  1448.                       command to store the value of the %0 substitution
  1449.                       variable in an environment variable before executing
  1450.                       the SHIFT command.
  1451.  
  1452.                       The SHIFT command can also be used even if you use
  1453.                       less than the maximum ten substitution variables.
  1454.  
  1455.           Examples:   To see how the SHIFT command works we have made a
  1456.                       simple batch file:
  1457.      REM Test of the SHIFT commandECHO %0 %1 %2SHIFTECHO %0 %1 %2SHIFTECHO
  1458.  
  1459.                                     %0 %1 %2
  1460.  
  1461.                       You will need to run this batch file with parameters
  1462.                       from the command line in order to see how it works.
  1463.                       We call this batch file TEST.BAT and execute it with
  1464.                       three parameters, number1, number2, and number3, like
  1465.                       this:
  1466.  
  1467.                           TEST number1 number2 number3
  1468.  
  1469.                       The output to your screen will look like this:
  1470.  
  1471.            TEST number1 number2number1 number2 number3number2 number3
  1472.  
  1473.  
  1474.  
  1475.  
  1476.  
  1477.  
  1478.  
  1479.  
  1480.  
  1481.  
  1482.  
  1483.  
  1484.  
  1485.  
  1486.  
  1487.  
  1488.  
  1489.                                       7-24
  1490.  
  1491.