if 块内的分配不起作用

Assignment inside a if block is not working

我需要从注册表中读取数据,将其加 1,然后将数据重写回注册表中

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
)

但是,这段代码的问题在于,它并没有增加COUNTER的数据。有什么解释吗?

谢谢!

具体...

@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!
)

或者,使用 CALL 和双 %%

@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%%
)