Dynamic variable re-evaluation in Windows batch

Viewed 487

I am a newbie in Windows batch scripting and probably this is a common question, answered multiple times. The problem I have is with the following script:

@echo off

setlocal ENABLEDELAYEDEXPANSION

set var1=1
echo var1 = %var1%
set var2=var1 init value : %var1%, var1 delayed value : !var1!

set var1=2
echo var1 = %var1%
echo var2 = !var2!

endlocal

The current output is:

var1 = 1
var1 = 2
var2 = var1 init value : 1, var1 delayed value : 1

, however the desired output should be:

var1 = 1
var1 = 2
var2 = var1 init value : 1, var1 delayed value : 2

How can this be achieved, any ideas? How can be the var2 variable somehow re-evaluated, after var1 changes its value? Thanks in advance!

2 Answers
Related