简体   繁体   中英

Switch Registry Value from Batch File

My requirement is that I need to change the value of a registry in particular this key "HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings" Value= ProxyEnable

The REG_DWORD ( 0 or 1 ) needs to be switched. ie SWITCH: IF proxy ENABLED then DISABLE IF proxy DISABLED then ENABLE

To Enable

reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyEnable /t REG_DWORD /d 1 /f

To Disable

reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyEnable /t REG_DWORD /d 0 /f

.

     @echo off
     setlocal
     set key="HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings"
     set value=ProxyEnable
     set newdata=0
     for /f "tokens=2* skip=3" %%a in ('reg query %key% /v %value%') do (
     set type=%%a
     set data=%%b
   )
      echo %data% | find /i "%newdata%" > nul
     if %errorlevel% equ 0 (echo %newdata% already present
     ) else (
     echo reg add %key% /v %value% /t %type% /d %newdata% /f
     )

this is the code so far, unable to modify it.

@echo off
setlocal
set key="HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings"
set value="ProxyEnable"
set newdata=0x0
For /f "tokens=2,3 skip=2 delims= " %%a in ('reg query %key% /v %value%') do (
set type=%%a
set data=%%b
)
echo  %data% | find /i "%newdata%"  > nul
if %errorlevel% equ 0 (echo %data% already present & reg add %key% /v %value% /t %type% /d 1 /f
) else (
echo %data% already present & reg add %key% /v %value% /t %type% /d 0 /f
)

To toggle/switch the value from 1 to 0 and 0 to 1, some simple maths helps simplify things: set /a newdata=1-data

Here's how I would do it:

@echo off
setlocal
set key="HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings"
set value=ProxyEnable
for /f "tokens=2* skip=2" %%a in ('reg query %key% /v %value%') do (
    set data=%%b
)
set /a newdata=1-data
reg add %key% /v %value% /d %newdata% /f

I thing you have to use skip=2 :

@echo off
set key="HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings"
set "value=ProxyEnable"
for /f "tokens=2* skip=2" %%a in ('reg query %key% /v %value%') do call:Treat %%a %%b
exit/b

:Treat
echo %2 | find /i "1" && echo already present || reg add %key% /v %value% /t %1 /d 1 /f

If you want to disable it if present just replace the echo already present by :

reg add %key% /v %value% /t %1 /d 0 /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