You can also post your commands in a batch file which will direct output to the file. Once done with the batch file, open the file.txt
Main Topics
Browse All TopicsI am calling a few CMD Line programs from a FoxPro routine.
I need a way to capture the %errorlevel% that is returned from the executed CMD Line program and use this information in the FOxPro program.
Can anyone tell me how to do this?
Thanks!
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Testing for a Specific Error Level in Batch Files
http://support.microsoft.c
http://www.experts-exchang
The problem is that COMMAND.COM (the shell) doesn't pass back the exit status of the last command. Also, some builtin commands like DIR don't set (change) errorlevel at all. The only way I could figure out to get the status is to use a batch file as a helper that checks errorlevel and either sends it back as the output or writes it to a temp file for later inspection. Here's an example...send the children out of the room:
REM HELPER.BAT
REM The command to be tested. Find will return 1 if the string
REM isn't found, 2 if it is. So to test, call HELPER using
REM HELPER.BAT as the arg and then some other file that
REM doesn't have "nauseum" in it.
find "nauseum" %1
REM Check errorlevel. This must be done in descending order
REM because errorlevel implies greater-than-or-equal. I only
REM care about three returns here so I didn't do all 255 codes.
REM If the commands you run don't have output it would
REM probably be easier to echo the exit status directly to
REM stdout and capure it in Perl with backticks.
if errorlevel 255 goto ERR255
if errorlevel 2 goto ERR2
if errorlevel 1 goto ERR1
rem no error
echo 0 >err.dat
goto DONE
:ERR255
echo 255 >err.dat
goto DONE
REM Ad nauseum here
:ERR2
echo 2 >err.dat
goto DONE
:ERR1
echo 1 >err.dat
:DONE
REM The errorlevel is now in ERR.DAT, the Perl program
REM can read that and do something.
http://www.experts-exchang
http://www.vb2themax.com/A
Summary: See how the Shell function really works and how you can replace it with a more flexible CShell class that support synchronous execution of an external program, as well as redirection of the shelled program's output to a file.
http://www.vb2themax.com/A
http://www.experts-exchang
You need to use ShellExecuteEx instead of ShellExecute. Wait (WaitForSingleObject) on the process until it is finished and call GetExitCodeProcess.
to echo the error to a log file
echo %ERRORLEVEL% > error.log
http://www.experts-exchang
the return code should be available in the ERRORLEVEL environment variable I guess. Try
echo Error = %ERRORLEVEL%
... so that you can see it. Then you can use things like
if "%ERRORLEVEL%" == "0" goto _allok
if "%ERRORLEVEL%" == "1" goto _error_1
if "%ERRORLEVEL%" == "2" goto _error_2
or things like
goto _error_%ERRORLEVEL%
:error_1
:: (handling for err 1 here)
:error_2
:: (handling for err 2 here)
are also possible I think.
Wow --
Ok I got a lot of information here. Let me try to makesense out of some of it.
It sounds like there is not an easy way to cature the %errorlevel% return code DIRECTLY in FoxPro.
I am aware of how to caputre this in a CMD promt (using the %errorlevel% variable). Let me see if writing a batch file will be accectable in the situtation.
This can be very simple.
In the batch file, directly after each dos command you want to check the error code, just put this line of code (the 1 represents the specific error in the batch file, for which you would have a seperate on each time you check for an error).
IF NOT %ERRORLEVEL%==0 ECHO 1-%ERRORLEVEL%>errresult.t
Directly after launching that batch file from VFP, issue these commands to determine if there was an error, and if there was, retrieve the batch file error number and the error code.
RUN mybatchfile.bat
IF FILE("errresult.txt") = .t
cErrResult = FILETOSTR("errresult.txt")
nBatFileErr = VAL(LEFT(cErrResult, AT("-", cResult) - 1))
nErrCode = VAL(SUBSTR(cErrResult, AT("-", cResult) + 1))
ENDIF
Business Accounts
Answer for Membership
by: CaptainCyrilPosted on 2005-06-22 at 13:01:03ID: 14278946
try to run the cmd line with > file.txt
after the call returns to foxpro
RUN dir > file.txt
MODIFY FILE file.txt NOMODIFY
DELETE FILE file.txt