Call one batch script in another batch script and perform operation on the values returned by first script
Asked Answered
V

1

1

I have a batch script "first.bat" that returns a list of values and an exit code. My task is to call this script in another script "second.bat" and perform the operations on the values returned by the "first.bat" only if the calling of first script has not returned an error code.

I have called the first.bat and stored its output in a text file. After that I check the result code and perform the operation by reading the text file if the result code is success code. And after the operation I delete the text file

Below is the code snippet of second.bat

@ECHO OFF


call first.bat >t

if /i %errorlevel%==100 (

 echo Performing operation

 for /F "tokens=1" %%a in (t) do echo %%a

 for /F "tokens=2" %%a in (t) do echo %%a

 del t

) else (
  echo Error occurred
)

I want to know can the same thing be done in some elegant way by not writing the output of first.bat to text file.

Veradi answered 23/4, 2013 at 7:42 Comment(1)
You can set some environment variables for retaining values. I don't see many alternatives.Floatage
I
1
@ECHO OFF
SETLOCAL
for %%i in (var return1 return2 bat1err) do set "%%i="
FOR /f %%i IN ('call first') DO SET var=%%i
FOR /f "tokens=1,2" %%i IN ("%var%") DO set return1=%%i&set return2=%%j

Should set return1 and return2 to your two values BUT the value of ERRORLEVEL generated by FIRST.bat can't be retrieved.

In your original code, the file t would be generated in any case - and would be zero-length if there was no output from first.bat. I'm confused by your action in only deleting t if no-error-occurs....

So - what we'd really need to do is to change FIRST.bat a little.

If first.bat does not use SETLOCAL, then

set bat1err=%errorlevel%

at an appropriate point would return bat1err set to the errorlevel.

If first.bat does use SETLOCAL, then life gets a little more complicated

set bat1err=%errorlevel%

at an appropriate point would set bat1err in the same way, but you would need to use

ENDLOCAL&set bat1err=%bat1err%

before exiting. This is a parsing trick, cashing in on the way in which the line is first parsed, then executed. What happens is that the line is actually executed as

endlocal&set bat1err=22

or whatever, setting BAT1ERR within the context of the calling, not the called batch.

Another way would be to include %errorlevel% in your output, and simply change the analysis to

FOR /f "tokens=1,2,3" %%i IN ("%var%") DO set bat1err=%%i&return1=%%j&set return2=%%k

OR, depending on quite what the output of first.bat is, you may be able to do it a third way:

If first.bat produces no output for error but a line of output for success,

FOR /f %%i IN ('call first') DO SET var=%%i
if defined var (
 FOR /f "tokens=1,2" %%i IN ("%var%") DO set return1=%%i&set return2=%%j
 ) else (echo Error occurred) 

and again return, return2 and var can be analysed with IF DEFINED to make decisions.

That really depends on information we don't have.

Interface answered 23/4, 2013 at 11:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.