Srijani Ghosh
Srijani Ghosh

Reputation: 4216

Assignment inside a if block is not working

I need to read a data from registry, add 1 to it and then rewrite the data back in registry

FOR /f "tokens=2*" %%a in ('reg query "HKLM\SOFTWARE\Looptest" /v "subcounter"') do set "subcounter=%%b"

if %subcounter% EQU 6 (
    set /a counter=%counter%+1
    echo increasing value for counter %counter% >> abc.log
    reg add HKLM\Software\Looptest /f /v counter /t REG_SZ /d %counter%
    pause
)

But,the problem in this code is that, it does not increase the data of COUNTER. Any explanation ?

Thanks !

Upvotes: 0

Views: 48

Answers (1)

soja
soja

Reputation: 1607

Specifically...

@echo off & setlocal enabledelayedexpansion
FOR /f "tokens=2*" %%a in ('reg query "HKLM\SOFTWARE\Looptest" /v "subcounter"') do set "counter=%%b"

if %counter% EQU 6 (
    set /a counter+=1
    echo increasing value for counter !counter! >> abc.log
    reg add HKLM\Software\Looptest /f /v counter /t REG_SZ /d !counter!
)

Alternatively, use CALL, and double %%.

@echo off
FOR /f "tokens=2*" %%a in ('reg query "HKLM\SOFTWARE\Looptest" /v "subcounter"') do set "counter=%%b"

if %counter% EQU 6 (
    set /a counter+=1
    call echo increasing value for counter %%counter%% >> abc.log
    call reg add HKLM\Software\Looptest /f /v counter /t REG_SZ /d %%counter%%
)

Upvotes: 2

Related Questions