Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return value from one batch file to caller batch file

I have a common .bat file that reads the status.xml file and finds out the value of the status field. This batch file is then called by other batch files for finding out status value. the calling batch files send the file name to the common bat file. I am not able to send the status from the common batch file to the calling batch files. Can someone please help?

main batch file
-- will call the common bat file and send the file name and a variable as arguments
setlocal
call Common.bat c:\folderdir\files\status.xml val1
-- trying to print the status returned by the common bat file
echo [%val1%]

common batch file
@ECHO off
setlocal EnableDelayedExpansion

rem will loop through the file and read the value of the status tag
(for /F "delims=" %%a in (%1) do (
set "line=%%a"
set "newLine=!line:<Interface_status>=!"
set "newLine=!newLine:</Interface_status>=!"
if "!newLine!" neq "!line!" (
  @echo Status is !newLine!
rem I want to send`enter code here` the value of newLine to the calling batch file
  set %~2 = !newLine!   <--this does not work
)

)) 
like image 496
user2593173 Avatar asked Jul 19 '13 15:07

user2593173


People also ask

How do you return a value from a batch file?

Syntax. It is common to use the command EXIT /B %ERRORLEVEL% at the end of the batch file to return the error codes from the batch file. EXIT /B at the end of the batch file will stop execution of a batch file. Use EXIT /B < exitcodes > at the end of the batch file to return custom return codes.

How do you return from a batch function?

Syntax. The return values are set in the function using the set command and the tilde(~) character along with the positional number of the parameter. Following example shows how a function can be called with return values.

What does %% do in batch file?

Use double percent signs ( %% ) to carry out the for command within a batch file. Variables are case sensitive, and they must be represented with an alphabetical value such as %a, %b, or %c. Required. Specifies one or more files, directories, or text strings, or a range of values on which to run the command.

What is Errorlevel in batch script?

Batch file error level: %ERRORLEVEL% is an environment variable that contains the last error level or return code in the batch file – that is, the last error code of the last command executed. Error levels may be checked by using the %ERRORLEVEL% variable as follows: IF %ERRORLEVEL% NEQ 0 ( DO_Something )


2 Answers

Within a SETLOCAL/ENDLOCAL bracket (where EOF=ENDLOCAL) ANY changes made to the environment are backed out.

You need to set a variable within Common.bat that is visible after the final close-parenthesis (ie. your return value - and it could be an empty string.

Then, in the line after common.bat's final close-parenthesis, put this line:

ENDLOCAL&set %~2=%returnvalue%

where returnvalue contains the er, value you wish to return (funny, that...)

BTW: A string SET is SPACE-SENSITIVE. Had the line worked, you would have been setting the variable "VAR1 " - not "VAR1" - the space before the = would have been INCLUDED in the variable name - and any spaces after the = likewise included in the value assigned.

The syntax

set "var=value"

is often used to exclude any stray trailing spaces on a line (as may be left by some editors)


(Sigh)...

@ECHO off
setlocal EnableDelayedExpansion

rem will loop through the file and read the value of the status tag
(for /F "delims=" %%a in (%1) do (
set "line=%%a"
set "newLine=!line:<Interface_status>=!"
set "newLine=!newLine:</Interface_status>=!"
if "!newLine!" neq "!line!" (
  @echo Status is !newLine!
rem SET THE RETURN VALUE
  set RETURNVALUE=!newLine!
)

)) 

ENDLOCAL&SET %~2=%RETURNVALUE%
like image 55
Magoo Avatar answered Oct 24 '22 03:10

Magoo


Peter Wright describes the main technique.

The last problem seems to be to exit the for loop without losing the value.

You can use a GOTO :break as GOTO stops all loops immediately.

It's not possible to use !newline! in the ENDLOCAL block, as this would expand after the ENDLOCAL, but then it's empty.

@ECHO off
setlocal EnableDelayedExpansion

for /F "delims=" %%a in (%1) do (
  set "line=%%a"
  set "newLine=!line:<Interface_status>=!"
  set "newLine=!newLine:</Interface_status>=!"
  if "!newLine!" neq "!line!" (
    @echo Status is !newLine!
    goto :break
  )
)

( 
  endlocal
  set "%~2=%newLine%"
)

If your value in newLine might contain quotes, then it's better to use a technique that's a bit safer:

for /F "delims=" %%a in ("!newline!") DO (
  endlocal
  set "%~2=%%~a" 
)
like image 38
jeb Avatar answered Oct 24 '22 03:10

jeb