简体   繁体   中英

Querying a registry key value via batch file

I'm trying to create a simple batch file that uses the reg query command to check for the existence of a value in a registry key, specifically

HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings ProxyEnable key.

If I run this command:

reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyEnable /f 0x1 /d

It returns the following:

HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings ProxyEnable REG_DWORD 0x0

It's as if it's not accepting my /f search field, as I would expect the command as entered to return no results since my value for that key is 0x0 .

I've tried using quotes around the " 0x1 ", and other combinations, but wondering if I'm doing something wrong.

Thanks in advance!

As you only appear to be wanting to know if enabling proxy is toggled on, you can use this instead:

@Echo Off

Set "RR=HKCU"
Set "RK=Software\Microsoft\Windows\CurrentVersion\Internet Settings"
Set "RV=ProxyEnable"

For /F "Skip=1 Tokens=2*" %%A In ('Reg Query "%RR%\%RK%" /V "%RV%" 2^>Nul'
) Do If %%~B Gtr 0 Echo(%%B

Pause

If you wanted to for instance to toggle it on if it is off then change it to something like this:

@Echo Off

Set "RR=HKCU"
Set "RK=Software\Microsoft\Windows\CurrentVersion\Internet Settings"
Set "RV=ProxyEnable"

For /F "Skip=1 Tokens=2*" %%A In ('Reg Query "%RR%\%RK%" /V "%RV%" 2^>Nul'
) Do If %%~B Equ 0 Reg Add "%RR%\%RK%" /V "%RV%" /T REG_DWORD /D 0x1 /F>Nul

Although technically you could just run this to do that:

@Echo Off

Set "RR=HKCU"
Set "RK=Software\Microsoft\Windows\CurrentVersion\Internet Settings"
Set "RV=ProxyEnable"

Reg Add "%RR%\%RK%" /V "%RV%" /T REG_DWORD /D 0x1 /F>Nul

Edit

To check it first using the method suggested by Squashman and improved by zett42, because the following EnableHttp1_1 REG_DWORD 0x0 would have been sent to Find :

@Echo Off

Set "RR=HKCU"
Set "RK=Software\Microsoft\Windows\CurrentVersion\Internet Settings"
Set "RV=ProxyEnable"

Reg Query "%RR%\%RK%" /F 1 /E /T REG_DWORD|Find /I "%RV%">Nul 2>&1||(
    Reg Add "%RR%\%RK%" /V "%RV%" /T REG_DWORD /D 0x1 /F>Nul)

Reg query is for read registry data not for set or change the registry data
to change the Registry data replace query with Add

reg add "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyEnable  /d 0x1 /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