REXX Tips & Tricks, Version 2.80


Inf-HTML [About][Toc][Index] 0.9b (c) 1995 Peter Childs


Using conditional command execution



For simple tasks you can use the unconditional and the conditional command 
execution of the CMD.EXE. 


+------------------------------+------------------------------+
|Command separator             |Meaning                       |
+------------------------------+------------------------------+
|command1 & command2           |Execute command1; execute     |
|                              |command2 regardless of the    |
|                              |exit code of command1         |
+------------------------------+------------------------------+
|command1 && command2          |Execute command1; execute     |
|                              |command2 only if the exit code|
|                              |of command1 is 0.             |
+------------------------------+------------------------------+
|command1 | command2           |Execute command1 and command2;|
|                              |pipe STDOUT of command1 into  |
|                              |STDIN of command2 (see Using  |
|                              |PIPEs and Pipes & Errorlevel) |
+------------------------------+------------------------------+
|command1 || command2          |Execute command1; execute     |
|                              |command2 only if the exit code|
|                              |of command1 is NOT 0          |
+------------------------------+------------------------------+
  
Note:  command1 and command1 can be an internal or external OS/2 command 
       or two or more commands separated with one of the separators. Using 
       parenthesis to group the commands is also possible. (constructions 
       like 

        
       (c1 && c2) || ( c3 && c4 || c5)
       
       
       are possible) 
 
 Examples: 
 

  
 @ECHO OFF
 REM *** Some examples for conditional command execution
 REM
 REM     Note: This commands only work in Batch programs!
 REM           (because of the format used for the placeholder)
 REM
 
 REM ------------------------------------------------------------------
 REM *** copy all files in the current directory into the directory
 REM     .\backup and print a success message for every file
 REM     succesfully copied
 REM
 @ECHO OFF
 for %%d in ( *.* ) DO (
   @copy %%d backup\*.* 2>NUL >NUL && ECHO. %%d saved.
                       )
 
 REM ------------------------------------------------------------------
 REM *** copy all files in the current directory into the directory
 REM     .\backup and print an error message for every file
 REM     that couldn't be copied
 REM
 @ECHO OFF
 for %%d in ( *.* ) DO (
   @copy %%d backup\*.* 2>NUL >NUL || ECHO. Error copying %%d!
                       )
 
 REM ------------------------------------------------------------------
 REM *** copy all files in the current directory into the directory
 REM     .\backup and print a success or an error message for each
 REM     file depending on the success of the copy command
 REM
 @ECHO OFF
 for %%d in ( *.* ) do (
   ( copy %%d backup\*.* 2>NUL >NUL && ECHO. %%d saved. ) || ECHO. Error saving %%d
                       )
 
 REM ------------------------------------------------------------------
 REM *** create a directory named BACKUP in the current directory,
 REM     copy all files in the current directory into the directory
 REM     .\BACKUP, delete all files succesfully copied and
 REM     print a success or an error message for each file
 REM     depending on the success of the copy and del commands
 REM
 REM     Note: Be aware that OS/2 starts a new CMD.EXE for each
 REM           command string in parenthesises!
 REM
 REM     Note: Maybe you must read it more than one time to
 REM           understand what's going on.
 REM           (or look at the REXX program doing the same)
 REM
 @ECHO OFF
 ECHO.
 ECHO. Saving the files *.* into the directory .\backup
 md .\backup 2>NUL >NUL & cd .\backup && ( cd .. & for %%d in ( *.* ) do (
   copy %%d backup\*.* 2>NUL >NUL && (
    del %%d 2>NUL >NUL && (
      ECHO. --- %%d saved and deleted. ) || (
        ECHO. -W- %%d saved but not deleted. ) ) || (
          ECHO. -E- Error saving %%d ) ) ) || (
            ECHO. -E- Error accessing the directory .\backup! )
 ECHO.
 
 
 
   
 Note:  Keep in mind that the maximum line length handled by the CMD.EXE 
        is 1024! 
   

Inf-HTML End Run - Successful