简体   繁体   中英

batch file if statement for removing/adding from registry

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

I've made a function:

@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

Here you go

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
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM