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
)
))
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.
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.
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.
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 )
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%
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"
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With