Sign in with
Sign up | Sign in
Your question

Batch file to read registry

Last response: in Windows XP
Share
June 19, 2011 2:55:37 AM

I'm trying to write a batch file to make a registry change during unattended setup. The key that needs to be added is in:

HKEY_LOCAL_MACHINE
SYSTEM
CurrentControlSet
Control
Network
{4D36E972-E325-11CE-BFC1-08002BE10318}
{0E59D167-1ECC-4D3C-89B9-4A5650C6CF4A}
Connection

The second SID above will change with each installation so I need to search everything below the first SID looking for the key Connection. There, I need to add the new value ShowIcon=1. It's not quite as complicated as it may sound. I'm using the following command in a batch file:

FOR /F "TOKENS=*" %%? IN ('REG QUERY HKLM\SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318} /S^|FINDSTR \\Connection$') DO REG ADD "%%?" /V ShowIcon /T REG_DWORD /D 1 /F

It steps through the output of the REG QUERY command, sending each line through FINDSTR looking for the word Connections. When it finds it, the current registry path is in the variable %%? which is then used in the DO instruction to REG ADD the new value ShowIcon.

The problem is that the variable %%? contains the ASCII value 12 just before the expected registry key so the DO command resolves to:

REG ADD "XHKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}\{0E59D167-1ECC-4D3C-89B9-4A5650C6CF4A}\Connection" /V ShowIcon /T REG_DWORD /D 1 /F

That X before HKEY is not really an X but a female symbol, ASCII code 012. I don't know how to type that here. It makes the key name invalid so the command fails. I don't know where that character is coming from or how to avoid or delete it. Are there any DOS or batch file experts around to help sort this out?
June 19, 2011 6:27:34 AM


Potentially silly question alert. :D  Have you tried omitting the opening bracket?


m
0
l
June 19, 2011 10:32:26 AM

Saga Lout said:

Potentially silly question alert. :D  Have you tried omitting the opening bracket?



By "bracket", do you mean the double quotes around the REG ADD command? I just tried it without those quotes and I still get the odd character just before HKEY.

Or did you mean some other bracket?
m
0
l
Related resources
June 19, 2011 6:27:56 PM



I meant this one in front of REG ('REG QUERY HKLM\

If that should be a double quotation mark, it shows up in Post #1 as a bracket and a single quote. In which language and Unicode is your keyboard set up?


m
0
l
June 20, 2011 3:56:39 PM

I see what you mean now. That is supposed to be a left parenthesis and a single quote. I tried it with double quotes and the string passed to the DO command is something like DO REG ADD REG QUERY HKLM... It's taking too much of the original queried line into the DO command. So single quotes evidently are required at that point.

I tried it without the parenthesis but I get a message that 'REG QUERY was not expected. I think that the FOR command requires the list to be in parentheses.

My keyboard is US English and the code page is 437.
m
0
l
June 20, 2011 9:14:49 PM

FIXED!

For reasons I do not fully understand, if I first write the output of the REG QUERY command to a temporary file then use FINDSTR on that file to find the line containing the word Connection, it works. It's not much different than filtering REG QUERY's output through FINDSTR directly, but for some reason writing the text to a file and reading it back eliminates the odd character I was seeing. Here's the solution courtesy of Allen2 on MSFN.org:

REG QUERY "HKLM\SYSTEM\CurrentControlSet\Control\Network\{4D36E972-E325-11CE-BFC1-08002BE10318}" /S >%temp%\tmp_reg_conn.txt
findstr \\Connection$ %temp%\tmp_reg_conn.txt >%temp%\tmp_net_conn.txt
FOR /F "delims=" %%i IN (%temp%\tmp_net_conn.txt) DO (REG ADD "%%i" /V ShowIcon /T REG_DWORD /D 1 /F)
del /q %temp%\tmp_net_conn.txt
del /q %temp%\tmp_reg_conn.txt

m
0
l
!