批处理文件if语句从注册表中删除/添加

时间:2012-11-22 02:14:07

标签: windows if-statement batch-file registry

我想知道是否有人可以告诉我如何创建一个批处理文件,如果它存在,则从注册表中删除键/值,如果不存在则添加它?

2 个答案:

答案 0 :(得分:0)

我已经发挥了作用:

@Echo OFF

:: By Elektro H@cker

REM CALL :REGIF "KEY\SUBKEY" 
REM or
REM CALL :REGIF "KEY\SUBKEY" /V "VALUE"

REM Examples:
Call :REGIF "HKLM\Key that fails"
Call :REGIF "HKCU\Software\Microsoft\Windows\Shell\BagMRU"
Call :REGIF "HKCU\Software\Microsoft\Windows\Shell\BagMRU" /V "MRUListEx"
Pause&Exit

:REGIF
REG QUERY %* 1>NUL 2>&1 && (
    REG DELETE %* /F 1>NUL
    Echo [+] Key deleted: %* | MORE
) || (
    REM added value is default SZ type.
    REG ADD %* /F >NUL
        Echo [+] Key added: %* | MORE
)
GOTO:EOF

答案 1 :(得分:0)

你去吧

set name=HKLM\SOFTWARE\key /v value
reg query %name%
if %errorlevel%==0 (
reg delete %name% /f
) else (
if %errorlevel%==1 reg add %name% /d "data" /f
)