Reputation: 25
I'm using a batch script for my pc at work to switch between two proxy servers. Currently I have 2 separate scripts for each of the proxies which I use accordingly to which server I want to connect.
reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" ^
/v ProxyServer /t REG_SZ /d proxyAddress:port /f
Both scripts are the same, just the address is different. I would like to create a single batch file that changes between the two stored addresses (if one is in use, change it to the other one). Is there a way to return a value from REG_SZ entry and evaluate it to stored values?
Thanks
Upvotes: 2
Views: 2804
Reputation: 178
Thanks to user6811411 answer I created a more "generic" version. I modified the script that you can inject the Proxy-Adresses from the command line. Makes it more versatile and you don't have to hardcode the adresses (maybe for security concerns or whatsoever).
Use it like that:
<PATH>\switch_proxy.bat proxyVal1 proxyVal2
@Echo off
set "Key=HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings"
Set "Val=ProxyServer"
Set "Typ=REG_SZ"
Set "Proxy1=%1"
Set "Proxy2=%2"
for /f "tokens=3" %%R in (
'Reg Query "%Key%" /v %Val% ^|find /i "%Val%"'
) do if "%%~R" equ "%Proxy1%" (
Reg add "%Key%" /v %Val% /t %Typ% /d "%Proxy2%" /f
) else (
Reg add "%Key%" /v %Val% /t %Typ% /d "%Proxy1%" /f
)
Upvotes: 0
Reputation:
I do like new lines and vars ;-) and have template for this.
You have to adapt Proxy1 and Proxy2 vars.
:: ToggleProxy.cmd
@Echo off
set "Key=HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings"
Set "Val=ProxyServer"
Set "Typ=REG_SZ"
Set "Proxy1=proxyAddress1:port1"
Set "Proxy2=proxyAddress2:port2"
for /f "tokens=3" %%R in (
'Reg Query "%Key%" /v %Val% ^|find /i "%Val%"'
) do if "%%~R" equ "%Proxy1%" (
Reg add "%Key%" /v %Val% /t %Typ% /d "%Proxy2%" /f
) else (
Reg add "%Key%" /v %Val% /t %Typ% /d "%Proxy1%" /f
)
Upvotes: 4
Reputation: 5874
Because new lines are boring anyways (I am sorry for the second line...):
@echo off
for /f "tokens=3 delims= " %%a in ('reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyServer /t REG_SZ ^| findstr "REG_SZ"') do set currentValue=%%a
if "%currentValue%"=="<proxySetting1>"(
REM set proxySetting2 here
) ELSE (
REM set proxySetting1 here
)
Using regQuery
you can actually search for your key value. The loop is required to parse the output and read the value into the variable currentValue
. You can then use this value to check whether proxy one is active or not and use your command above to change it accordingly.
Explanation of the loop:
The output of the query look like this for me:
KEY_ROOT_HERE\PATH\TO\KEY
Name REG_TYPE VALUE
So with findstr REG_SZ
we only get the lower line of both. Then we have to take the third part of the string seperated by spaces (see the beginnning where it says "tokens=3 delims= "
and the longest part is the query itself mostly taken from your question.
Upvotes: 1