If you want to use the actual environment variable inside this for
block, you need a delayed extension:
setlocal enabledelayedexpansion for %%i in (*.txt) do ( set size=0 set /A size=%%~zi echo %%i !size! if !size! GTR 0 echo greater )
Note that you need to replace %size%
with !size!
to cause slow expansion instead of the usual.
The problem is that ordinary environment variables expand when the operator is parsed; the for
block is the only expression in this regard. Therefore, as soon as the loop starts the size
value before using the loop.
A delayed extension (c !
) Will expand the variables immediately before execution, and not during the analysis of the statement, so you will see the updated value from the loop.
source share