OS/2 Batch File Commands

The Batch File Commands are listed here alphabetically, you may browse through them or you may click on the links below.


  • CALL
  • ECHO
  • ENDLOCAL
  • EXTPROC
  • FOR
  • GOTO
  • IF
  • PAUSE
  • REM
  • SETLOCAL
  • SHIFT

  • CALL
    Calls a batch file from within another batch file without ending the first one. A batch file can then be used as commands from within a master batch file.

    CALL ---------- batchfile-------------|------------|-----| |- argument -| Related Commands: Echo EndLocal ExtProc For GoTo If Pause Rem SetLocal Shift

    It is important to note that:
    • Piping and redirection should not be used with the CALL command.
    • You can use CALL from any line inside a batch file.
    • The number of batch files is limited only by available memory. In an OS/2 session, a batch file should not call itself. If it does, it runs out of stack space and ends. In a DOS session, a batch file can call itself, but make sure that the batch file eventually ends.
    • CALL causes the data structure and file pointer of the currently running batch file to be saved, and a new data structure to be created. When the called batch file ends, the original batch file continues its processing with the statement following CALL.
    • Batch-file parameters can be passed to another batch file with CALL.

    As an example, assume that you have two batch files. Your first batch file is named EXAMPLE1.CMD, which contains these commands:

    ECHO EXAMPLE1.CMD is running ... CALL EXAMPLE2 ECHO EXAMPLE1.CMD is running again ... Your second batch file, the one you want to call, is EXAMPLE2.CMD. It contains: ECHO EXAMPLE2.CMD is running now ... If you are in the root directory when you type EXAMPLE1 at the OS/2 command prompt, the following is displayed on your screen: [C:\] ECHO EXAMPLE1.CMD is running ... EXAMPLE1.CMD is running ... [C:\] ECHO EXAMPLE2.CMD is running now ... EXAMPLE2.CMD is running now ... [C:\] ECHO EXAMPLE1.CMD is running again ... EXAMPLE1.CMD is running again ...
    ECHO
    Allows or prevents the screen display of OS/2 commands as they are run from a batch file. ECHO does not interfere with messages produced while commands are running.

    ECHO -----------------|----------------|-------------------| |------ ON -----| |------ OFF -----| |---- message ---| Related Commands: Pause Rem

    Enter this command without a parameter to display the current ECHO state.

    You can control (|) and redirect (>) output from a batch file.

    To prevent DOS from interpreting the | and > symbols, enclose them in double quotes. ECHO OFF ECHO PIPING "|" ECHO REDIRECTION ">" To prevent the OS/2 operating system from interpreting the | and > symbols, precede them with a ^ symbol. The ^ symbol tells the system to interpret the next character input, but not to display it. ECHO OFF ECHO PIPING ^| ECHO REDIRECTION ^> You can also control how batch-file lines or command lines are displayed. To prevent the system from displaying a batch-file line, place an @ before the command. @ECHO ON @REM THIS LINE IS NOT DISPLAYED REM THIS LINE IS DISPLAYED @FILE1 To display lines of text in a batch file, but prevent the display of command lines, follow this example: @ECHO OFF REM THIS LINE WILL NOT BE DISPLAYED ECHO THIS LINE WILL BE DISPLAYED In the following example, the system first displays ECHO OFF but does not display the REM or first DIR C:\OS2\SAMPLE.ABC commands because ECHO is OFF. However, the system still displays the output of the DIR command. Next, ECHO ON is processed and the system displays the DIR C:\OS2\SAMPLE.ABC command with the DIR output. ECHO OFF REM **** COMMAND DISPLAY IS NOW OFF DIR C:\OS2\SAMPLE.ABC ECHO ON DIR C:\OS2\SAMPLE.ABC When the above batch file is run, the following is displayed: ECHO OFF The volume label in drive C is OS2. Directory of C:\ SAMPLE.ABC 1234 7-17-88 12:14p 1 File(s) 141312 bytes free DIR C:\OS2\SAMPLE.ABC The volume label in drive C is OS2. Directory of C:\ SAMPLE.ABC 1234 7-17-88 12:14p 1 Files(s) 141312 bytes free
    ENDLOCAL
    Restores the drive, directory, and environment variables that were in effect before the SETLOCAL command was processed.

    ENDLOCAL --------------------------------------------------| Related Commands: SetLocal

    This command ends the SETLOCAL command, restoring the previous drive, directory, and environment settings. You can issue an ENDLOCAL command even if no SETLOCAL command is running.

    If you want to insert the ENDLOCAL command within a batch file to restore the drive, directory, and environmental variables, follow this example: PATH SETLOCAL A: CD \XYZ PATH A:\;A:\MISCPGM PATH ENDLOCAL PATH Note that the PATH command with no parameters displays the current value for PATH. You can see that the original current drive, directory, and environment value for PATH are saved when SETLOCAL is issued, and restored when ENDLOCAL is issued.


    EXTPROC
    Defines an external batch processor for a batch file.

    EXTPROC |---------|-|--------|- filename -|-------------|--| |- drive -| |- path -| |- arguments -| This command is useful if you have your own batch processor and want to substitute it for the OS/2 batch processor.

    You must include the EXTPROC command as the first statement in any batch file you want processed by your batch processor. CMD.EXE calls your batch processor to process your batch-file statements.

    For example, if you wanted to use a batch processor called MYBATCH.EXE, located in the C:\BATCH directory, to run some of your batch files, type the following in your batch file: EXTPROC C:\BATCH\MYBATCH.EXE
    FOR
    Allows repetitive running of OS/2 commands.

    To use FOR from the OS/2 command prompt: FOR --- % variable --- IN --- (set) --- DO ---- command ---| To use FOR from a batch file: FOR ------ %%c ------- IN ---- (set) ---- DO ---- command -| It is important to note that:
    • The items in parentheses specify the "set," which can include file and path names.
    • An item in the set can contain the global file-name characters * or ?.
    • Use only one percent sign ( % ) before the variable if you are processing from the command prompt; use two percent signs ( %% ) if you are using the variable in a batch file.
    • For OS/2 sessions, piping and redirection can be used with the FOR command. The following example shows three C-language program files being compiled and compiler messages being saved in three files that have a .OUT extension. FOR %1 IN ( FILE1 FILE2 FILE3 ) DO CL /C %1.C > %1.OUT 2>&1
    • For DOS sessions, piping and redirection are not used with the FOR command.
    • For OS/2 sessions, you can specify more than one FOR command at a command prompt. For example, type the following in order to print these files: FILE1.C, FILE1.LST, FILE2.C, FILE2.LST, FILE3.C, FILE3.LST: FOR %1 IN ( FILE1 FILE2 FILE3 ) DO FOR %J IN ( C LST ) DO PRINT %1.%J

    In the following example, FOR sequentially sets the %%D parameter to each item in the set and then evaluates the command you want to run (TYPE).

    Assume you want a batch file to process the commands, TYPE FILE1 and TYPE FILE2, and have the result be the same as if you typed both commands from the command prompt: FOR %%D IN (FILE1 FILE2) DO TYPE %%D Assume you want to process the commands TYPE FILE1 and TYPE FILE2 from the command prompt: FOR %H IN (FILE1 FILE2) DO TYPE %H
    GOTO
    Transfers control to the line that follows the one containing the appropriate label.

    GOTO --------------------- label -------------------------| Related Commands: If

    It is important to note that:
    • A label is a set of characters within your batch file that indicates what action the operating system is to take. It can be a name or a string of characters, the first eight characters being significant (making it different).
    • A label is indicated by a colon (:), followed by the label name. If you specify a label that is not defined in the batch file, the current processing of the batch file ends.
    • A label cannot contain a period (.).
    • The operating system does not display labels within a batch file while it processes the batch file. Therefore, nonreferenced labels provide a convenient way for placing comments (within your batch file) that the operating system does not display when it processes the file.


    IF
    Allows conditional processing of OS/2 commands. When the condition is true, the operating system processes the command; otherwise, it skips the command and processes the next command in the file.

    Piping and redirection should not be specified on the IF command line.

    IF --|-------|--|- ERRORLEVEL --- number -----------|------ |- NOT -| |- string1==string2 ----------------| . . . . . . ----|-----------------------------------------------|----- |- EXIST --|---------|--|--------|-- filename --| |- drive -| |- path -| . . . . . .----------- command ------------------------------------| For readability, the file name parameters are shown on a separate line in this diagram. When you type the IF command, you can specify either an error level, string, or a file name.

    Related Commands: GoTo


    PAUSE
    Suspends processing of the batch file and displays the following message: Press any key when ready . . . PAUSE ------------|-----------|----------------------------| |- comment -| Related Commands: Echo Rem<

    Enter this command to display the message: Press any key when ready.... You can control how much of a batch file you want to process by placing PAUSE commands at strategic points in the file. At each PAUSE statement, the system stops and gives you time to decide whether to stop the processing. Press and hold Ctrl+Break, and type Y to stop a batch file from processing. In a DOS session, press any key to continue processing. In an OS/2 session, you cannot continue processing.


    REM
    Adds comments or line spacing in a batch file or a CONFIG.SYS file.

    REM --------------|-----------|----------------------------| |- comment -| When adding remarks or line spacing in a batch file, it is important to note that:
    • The OS/2 operating system displays the remark when batch processing reaches the REM command. If ECHO is OFF, the system does not display the remarks.
    • You can use REM by itself to improve the readability of your batch file. The OS/2 operating system treats the preceding REM commands as comments only and does not attempt to act on the comments.

    You can use the REM command to add remarks or line spacing to your CONFIG.SYS file. Any lines of text added using REM are ignored by the system during CONFIG.SYS processing and are not displayed on your display terminal.


    SETLOCAL
    Lets you define the drive, directory, and environment variables that are local to the current batch file.

    SETLOCAL --------------------------------------------------| Related Commands: EndLocal<

    This command saves the current drive, directory, and environment variables and lets you define local variables for the batch file. The previous drive, directory, and environment settings are restored when the ENDLOCAL command is encountered, or when the batch file ends.

    If a matching ENDLOCAL command is not found, the saved elements are restored when the processing of the batch file that issued the SETLOCAL command ends. In this way, any environment variable and the current drive and directory can be altered without affecting the command processor. Issuing multiple SETLOCAL commands without matching ENDLOCAL commands is not considered an error.


    SHIFT
    Allows command lines to use more than 10 replaceable parameters in batch file processing.

    SHIFT -----------------------------------------------------| It is important to note that:
    • Command files are limited to handling 10 parameters, %0 through %9, unless you use the SHIFT command.
    • All parameters on the command line are shifted one position to the left, the %1 parameter replacing the %0 parameter, the %2 parameter replacing the %1 parameter, and so on. Each following shift command causes all the parameters to be shifted to the left by one position.
    • There is no backward shift. Once SHIFT is run, the %0 parameter that existed before the shift cannot be recovered.

    As an example, assume: A SHIFT results in the following: %0 = 'A' %0 = 'B' %1 = 'B' %1 = 'C' %2 = 'C' %2 - %9 are empty %3 - %9 are empty