Why are you enclosing variable names in pairs of %
signs? The usual syntax for reading a variable is %var1%
.
set /A
can read variables even without percent signs around, so use set /a b=var1 %% 25
.
The other set /A
command line can be changed to set /A var1+=1
.
There is one problematic variable in your code, namely b
, because you are writing and reading it within the same (parenthesised) block of code, so %b%
is read when the entire for
loop is parsed, at which point it is empty most probably.
For such cases, you need to apply delayed expansion, so the variable is read at execution time. To enable it, use the setlocal
command; to actually use it, write !b!
instead of %b%
.
Here is the fixed code:
set /A var1=0
for /R "folder" %%G in (.) do (
pushd "%%~G"
set /A b=var1 %% 25
setlocal EnableDelayedExpansion
if !b! EQU 0 (
timeout /T 3
)
endlocal
set /A var1+=1
popd
)
Here I placed setlocal
and endlocal
inside the loop so that delayed expansion is disabled during expansion (reading) of the for
loop variable %%G
, because in case it contains exclamation marks, they would get lost otherwise.