Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Returning a Value to a Batch File 1

Status
Not open for further replies.

SGLong

Programmer
Jun 6, 2000
405
US
We have a number of VFP programs that are launched automatically at various times throughout the day by a program called Active Batch. Active Batch uses DOS Batch Files to trigger the VFP programs. Is there any way that VFP can send a success code (i.e. 0 = Pass, -1 = Fail), just before the QUIT command, to a batch file so the batch file can pass it on to Active Batch?

Steve
 
You mean a 'return code' or 'error code' like DOS programs can.

I don't think VFP apps can do this, so you would probably need to get your VFP app to write out a text file (to the same folder as the program ran from probably) and then use a simple application (perhaps in Clipper, C or VB) that reads the text file and sets the errorlevel to suit.

I've written one, you are perfectly free to use it.

Email me martin (at) finedata dot com





Regards

Griff
Keep [Smile]ing
 
Steve,

I think the answer is to use the ExitProcess API call. Here's the DECLARE for it:

Code:
DECLARE ExitProcess IN Win32API INTEGER nExitCode

The value of the exit code is the value that the batch file's ERRORLEVEL will pick up.

Keep in mind that this function will kill your application stone dead, so make sure you have cleaned up completely before you call it.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

My Visual FoxPro site: www.ml-consult.co.uk
 
I was just about to post that myself!

Well done Mike!

Code:
DECLARE ExitProcess in Win32API INTEGER ExitCode

After closing your databases and issuing a CLEAR EVENTS and FLUSH, call ExitProcess with an integer value in the range -(2^31) to (2^31) - 1 . 

=ExitProcess(nReturnCode) && nReturnCode can be a constant

Fount it after a search on google

Regards

Griff
Keep [Smile]ing
 
Martin,

I used ExitProcess() many years ago, and wasn't 100 percent sure I had the syntax right, but you've confirmed it.

One small point: You say the range of values is -(2^31) to (2^31) - 1. That might be true, but I believe the ERRORLEVEL construct expects an 8-bit positive integer (IOW, 0 - 255). Steve might need to adjust his batch file to cater for that.

Mike


__________________________________
Mike Lewis (Edinburgh, Scotland)

My Visual FoxPro site: www.ml-consult.co.uk
 
Would something like this work:

<code>
PROCEDURE RunShellScript
LPARAMETERS tcCommand, tlSilentMode, tcRetVal

IF EMPTY(m.tcCommand)
RETURN
ENDIF

LOCAL oShell, llError, loException, lnErrorCode
llError = .F.
TRY
oShell = CREATEOBJECT("WScript.Shell")
CATCH TO loException
IF m.tlSilentMode
tcRetVal = "Run-time error: WScript.Shell Object is not Found"
ELSE
=MESSAGEBOX("Run-time error: WScript.Shell Object is not Found")
ENDIF
llError = .T.
ENDTRY
IF m.llError <> .T.
TRY
lnErrorCode = oShell.RUN(m.tcCommand, 0,.T.) && SW_SHOW_HIDDEN 0,SW_SHOW_NORMAL 1,SW_SHOW_MINIMIZED 2,SW_SHOW_MAXIMIZED 3
IF m.lnErrorCode <> 0
tcRetVal = "Error in running shell command. Error Code: " + TRANSFORM(m.lnErrorCode)
ENDIF
CATCH TO loException
IF m.tlSilentMode
tcRetVal = "Run-time error: WScript.Shell: " + ;
m.tcCommand + CHR(13) + CHR(10) + Log_Error(m.loException)
ELSE
=MESSAGEBOX("Run-time error: WScript.Shell: " + m.tcCommand)
ENDIF
llError = .T.
ENDTRY
ENDIF
RELEASE oShell

RETURN !m.llError</code>
 
Ilyad,

I must be missing something. I can't see how your code returns the value to the batch file. As far as I can see, you are bringing the value into your VFP program, whereas what Steve wants to do is to send the value from VFP to the batch file.

By all means, correct me if I'm wrong.

Mike


__________________________________
Mike Lewis (Edinburgh, Scotland)

My Visual FoxPro site: www.ml-consult.co.uk
 
Mike,

My bad. I thought the problem was to get the Error code in VFP, somehow misread the question.

Also sorry for off-topic, but can you please tell me, how can I properly format code samples? I'm not sure which tag do I have to use.
 
you can use code in square brackets to start and then /code to stop

Code:
BLAH BLAH



Regards

Griff
Keep [Smile]ing
 
Thanks all... Mike, it looks like ExitProcess is what I want to do. I'll test it in the morning and let everybody know the results.

Steve
 
Hey Mike,

You've been around a while.

In the Foxbase/ FP-DOS days we used to be able to exit applictions with

QUIT <exprn>

and set ERRORLEVEL for a .bat file. Do you remember when that dropped off? Tamar, if you're lurking, feel free to chime in.

Dan
 
Hi Dan,

You've been around a while.

I guess it really shows <g>.

In the Foxbase/ FP-DOS days we used to be able to exit applictions with ... QUIT <exprn> ... and set ERRORLEVEL for a .bat file.

Well, I didn't know that. It's not mentioned in my FoxBase 2.41 manual (I have it in front of me) or in my seminal 1989 work on dBASE (sales of which reached triple figures).

As you say, I'll bet Tamar knows.

Mike


__________________________________
Mike Lewis (Edinburgh, Scotland)

My Visual FoxPro site: www.ml-consult.co.uk
 
You certainly could in Clipper.

B-)

Regards

Griff
Keep [Smile]ing
 
Well, Mike was right... it killed my application dead. So dead, in fact, that it closed the DOS Command Window and the batch file at the same time. The batch file never got the chance to respond to and process the return code.

Here's what I have...
The VFP Program:
Code:
*-- FILE:  Batch_Test.PRG
CLOSE TABLES ALL 
CLEAR

WAIT WINDOW "Opening table..." NOWAIT 
USE c:\myprogramfolder\disclaimers 


DECLARE ExitProcess IN WIN32API INTEGER ExitCode

GO BOTTOM
? RECCOUNT(), RECNO()
WAIT WINDOW "Terminating..." TIMEOUT 2

nExitCode = 1
CLOSE TABLES ALL 

=ExitProcess(nExitCode)

and here's the batch file.

Code:
net use N: \\server\programs
n:
cd n:\vfp6
cd c:\myprogramfolder
IF ERRORLEVEL 1 GOTO LabelSucess
IF ERRORLEVEL 2 GOTO LabelFailure

n:\vfp6\vfp6.exe c:\myprogramfolder\batch_test.fxp %1

ECHO No Return Detected
SLEEP 5


:LabelSuccess
ECHO Success Detected
SLEEP 5

:LabelFailure
ECHO Failure Detected
SLEEP 5

Do you see anything that I can change so the batch file captures the code that is passed back from VFP?

Steve
 
Hi Steve,

I would try putting the ERRORLEVEL statements after the line that runs your VFP app.

pamela
 
Pamela is right, your errorlevel assessment needs to be AFTER your VFP program is run.

Also note that errorlevels need to be measured in descending order - the test is not really for EQUALS, it is for GREATER THAN OR EQUALS.

Also note that your code will 'fall through' all the errorlevel coding unless you trap it:

Code:
net use N: \\server\programs
n:
cd n:\vfp6
cd c:\myprogramfolder

n:\vfp6\vfp6.exe c:\myprogramfolder\batch_test.fxp %1

IF ERRORLEVEL 2 GOTO LabelFailure
IF ERRORLEVEL 1 GOTO LabelSucess

ECHO No Return Detected
SLEEP 5
GOTO LabelTheEnd

:LabelSuccess
ECHO Success Detected
SLEEP 5
GOTO LabelTheEnd

:LabelFailure
ECHO Failure Detected
SLEEP 5

:LabelTheEnd

Regards

Griff
Keep [Smile]ing
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top