Lastwish
Lastwish

Reputation: 327

Batch Script, Null value in Reg key returns Error

The below code acquires the IP of IE proxy. Incase there arent any specified, wanted it to return NIL.

FOR /F "usebackq tokens=3*" %%B IN (`REG QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v  ProxyServer`) DO ( 
    SET ProxyAddress=%%B
    IF "!ProxyAddress!"=="" (
        SET "ProxyAddress=NIL"
    )
Echo %ProxyAddress%

But this returns an error, The system was unable to find specified reg key.

Upvotes: 0

Views: 424

Answers (1)

JosefZ
JosefZ

Reputation: 30218

This script should help (set value to ProxyAddress variable in advance).

Note 2^>NUL will redirect error messages to NUL (a bottomless pit), cf. redirection. Note > is escaped with ^ here.

SET "ProxyAddress=NIL"
FOR /F "usebackq tokens=3*" %%B IN (
  `REG QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v  ProxyServer 2^>NUL`
) DO ( 
    SET "ProxyAddress=%%B"
)
Echo %ProxyAddress%

Upvotes: 1

Related Questions