Consultor Eletrônico



Kbase 21039: How To Trap an OS Error When Using the OS-COMMAND Statement
Autor   Progress Software Corporation - Progress
Acesso   Público
Publicação   6/25/2009
Status: Verified

GOAL:

How to trap an Operating System error when using the OS-COMMAND Statement

GOAL:

How to verify if a command executed using OS-COMMAND Statement was executed or not

FACT(s) (Environment):

Windows 32 Intel
Windows NT 32 Intel/Windows 2000

FIX:

One of the principal uses for the OS-COMMAND Statement is to execute an Operating System command from a Progress procedure. However, when that Operating System command fails, the error is never returned for the OS-COMMAND Statement, even when OS-ERROR is used with it. The OS-COMMAND Statement always sets the value for that function to 0 (zero), whether or not an error occurs.

A workaround is to execute an Operating System batch file with the OS-COMMAND Statement. When that OS batch file gets executed, it will in turn run the desired Operating System command and verify whether or not an OS error has occurred afterwards. If the OS error has happened, the batch file will then generate a text file that will eventually be input into Progress, thus it will know that the Operating System command executed by the batch file has failed.
Modules, osCommand.p and error.bat, provide an example of how to do this.

/* osCommand.p */

DEFINE VARIABLE vcError AS CHARACTER NO-UNDO.
DEFINE STREAM inErrStream.

IF SEARCH("error.bat":U) <> ? THEN
DO:
OS-COMMAND SILENT VALUE(SEARCH("error.bat":U)).
INPUT STREAM inErrStream FROM VALUE(SEARCH("error.txt":U)).
IMPORT STREAM inErrStream DELIMITER ",":U vcError NO-ERROR.
IF ERROR-STATUS:ERROR THEN
MESSAGE "Error importing stream!":U VIEW-AS ALERT-BOX ERROR.
INPUT STREAM inErrStream CLOSE.
IF vcError BEGINS "ERROR":U THEN
MESSAGE vcError " occurred while executing an OS Command!":U
VIEW-AS ALERT-BOX ERROR.
OS-DELETE VALUE(SEARCH("error.txt":U)).
IF OS-ERROR <> 0 THEN
MESSAGE "Error deleting file ":U SEARCH("error.txt":U)
VIEW-AS ALERT-BOX ERROR.
END.

/* error.bat */

@echo off
nprint c:\test q=hp4000 c=1
if %ERRORLEVEL% == 0 goto end
echo ERRORLEVEL#: %ERRORLEVEL% > c:\error.txt
:end