Variable doesn't get updated in the function of Batch file

后端 未结 2 878
臣服心动
臣服心动 2020-12-22 04:25

I have a simple batch script that will read the value in file version.property and perform a certain job my code is below

TITLE StartEODMaintenance
echo o         


        
2条回答
  •  不思量自难忘°
    2020-12-22 04:50

    The %variables% are expanded when the command is parsed and the command if followed by a block is parsed at once. So when you set a variable in the block, its following usage (in echo here) has been expanded already and it results in showing the value that was valid before entering the block!

    You need delayed expansion to read variable values at the moment when they are used, not parsed. First, issue the command:

    setlocal EnableDelayedExpansion
    

    Then refer to the variables modified earlier within the same block as !variable!.

    if %errorlevel%==0 (
    echo "errorlevel 0" 
    set "Build=%~1"
    echo build value in function !Build!
    )
    

提交回复
热议问题