'check ProxyEnable in a batch file

I currently have two batch files: proxy_enable.bat

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

and proxy_disable.bat

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

I would like to make only one file which would be proxy_switch.bat and that would be:

if (registry key is 0) (put it to 1) ELSE (put it to 0)

But I can't find a way to read the ProxyEnable key that is in HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings



Solution 1:[1]

The following script should toggle the value, each time it is run:

@Echo Off
SetLocal EnableExtensions DisableDelayedExpansion

Set "Fnd=%SystemRoot%\System32\find.exe"
Set "Reg=%SystemRoot%\System32\reg.exe"
Set "Key=HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings"
Set "Val=ProxyEnable"
Set "Typ=REG_DWORD"

%Reg% Query "%Key%" /V "%Val%" | %Fnd% "x0" 1>NUL && (
    %Reg% Add "%Key%" /V "%Val%" /T %Typ% /D "1" /F 1>NUL
) || %Reg% Add "%Key%" /V "%Val%" /T %Typ% /D "0" /F 1>NUL

Solution 2:[2]

reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyEnable" | find /i "0x0" && goto :set1
:set0
set it here to 0
goto :end
:set1
set it here to 1
:end

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Compo
Solution 2