Reputation: 2502
Is there a way to retrieve and display the text of a Windows error message inside of a CMD script?
For example, assume MYAPP.EXE returns 2 (ERROR_FILE_NOT_FOUND). The Windows error message associated with this is: "The system cannot find the file specified"
Can I retrieve and display that within my CMD script? For example,
REM mycmd.cmd
MYAPP.EXE
ECHO %ERRORMESSAGE%
Thanks.
Upvotes: 3
Views: 1744
Reputation: 80023
FOR /f "delims=" %%a IN ('COPY /y "%sourcedir%\xyz" "%sourcedir%\pol" 2^>^&1') DO SET "copyresult=%%a"
ECHO %ERRORLEVEL% %copyresult%
Assuming the source file does not exist, then the error message should be redirected from device 2 (stderr) to device 1 (stdout) and hence be applied by the for
to %%a
and thence copyresult
.
This produces errorlevel
1 for the copy
case.
If you use a non-existent command like czopy
in place of copy
, then errorlevel
will be 9009 but since the output is on two lines,
'CzOPY' is not recognized as an internal or external command,
operable program or batch file.
only the last is assigned to copyresult
Upvotes: 0
Reputation: 70923
Asumming your program returns a standard windows system error code as exit code
myapp.exe
net helpmsg %errorlevel%
Upvotes: 6