Variable doesn't get updated in the function of Batch file -
i have simple batch script read value in file version.property , perform job code below
title starteodmaintenance echo off cls set "build=0" call:findstring "mainalgo" if /i "%build%" == "mainalgo" ( echo "start job on mainalgo" ) else ( call:findstring "drsitealgo" echo build value %build% if /i "%build%" == "drsitealgo" ( echo "start job on secondalgo" ) else ( echo "sth wrong" ) ) :findstring echo funtioninput %~1 find /i /c "%~1" version.property if %errorlevel%==1 ( echo "errorlevel 1" set "build=0" ) if %errorlevel%==0 ( echo "errorlevel 0" set "build=%~1" echo build value in function %build% ) :end
the content in version.property below
drsitealgo
the problem found when program executed looks below line work incorrectly. variable build not set value in "%~1"
set "build=%~1"
i got below output
funtioninput mainalgo ---------- version.property: 0 "errorlevel 1" funtioninput drsitealgo ---> %~1 show correct value, drsitealgo ---------- version.property: 1 "errorlevel 0" build value in function 0 ---> here wrong! build variable somehow didn't updated, suppose drsitealgo build value 0 "sth wrong"
not sure have set make works??
the %variables%
expanded when command parsed , command if
followed block parsed at once. when set variable in block, following usage (in echo
here) has been expanded , results in showing value valid before entering block!
you need delayed expansion read variable values @ moment when used, not parsed. first, issue command:
setlocal enabledelayedexpansion
then refer variables modified earlier within same block !variable!
.
if %errorlevel%==0 ( echo "errorlevel 0" set "build=%~1" echo build value in function !build! )
Comments
Post a Comment