Solved Batch file to set variable registry keys as variables

February 13, 2013 at 11:25:11
Specs: Windows 7

Part of the maintenance we do at my work involves rebuilding user's profiles and in windows 7 the registry key for the old profile needs to be deleted before the new profile will work correctly.

The problem I'm running into is that in order to get the correct key to delete I need to run a query on the user's profile ID (the ID is not part of the actual key) but when either exporting to a text file or using a FOR command only the very last part of the result is being returned.

So when I run:

reg query "HKLM\SOFTWARE\WOW6432NODE\MICROSOFT\WINDOWS NT\CURRENTVERSION\PROFILELIST" /s /f %curuser%>profilerebuild.txt

setlocal enabledelayedexpansion
set rukey=
FOR /F "tokens=1,2 delims= " %%A IN (profilerebuild.txt) DO set rukey=%%A

Echo Your current registry profile path is: %rukey%

(where %curuser% is set earlier in the batch file) what I get returned as %rukey% is "End of search: 1 match(es) found." rather than the key.

The confusing part is that when I have the FOR command ending with "...DO ECHO %%A" instead of "DO SET rukey=%%A" it echo's and displays the key.

Is there something that I'm overlooking with this?


See More: Batch file to set variable registry keys as variables

Report •


#1
February 13, 2013 at 12:16:09

If you're running this as the current user, I'd suggest using %userprofile%.

How To Ask Questions The Smart Way


Report •

#2
February 13, 2013 at 12:29:58

I have a separate login script to launch a variety of batch files with admin rights depending on what needs to be done.

I get the same results even when I attempt to run this with my own profile so I know there's most likely something wrong with either the syntax or just the ability to capture the full key as the variable.

I was just hoping someone could look at the syntax and maybe figure out where I messed up or figure out what I overlooked, thank you for the response though.


Report •

#3
February 13, 2013 at 12:50:47
✔ Best Answer

Your problem is that FOR . . .SET= syntax always grabs the last line, and not the first. I suggest you change the (profilerebuild.txt) part of your FOR loop to:
('reg query "HKLM\SOFTWARE\MICROSOFT\WINDOWS NT\CURRENTVERSION\PROFILELIST" /s ^| findstr /i %curuser%')

How To Ask Questions The Smart Way


Report •

Related Solutions

#4
February 13, 2013 at 13:18:22

Your code works better in that it does not return the very last line, but the issue is now that it's showing the values:

ProfileImagePath REG_EXPAND_SZ C:\Users\%curuser%

instead of returning the actual key. Which has been the problem I'm running into since the beginning.

I tried changing it to

"reg query "HKLM\SOFTWARE\MICROSOFT\WINDOWS NT

\CURRENTVERSION\PROFILELIST" /s /f %curuser% ^| findstr /i HKLM"

to grab the line with the key on it, but still no luck on that either. the variable %rukey% turns up a blank variable.

EDIT: i suppose I should also let you know that I have tried outputting the reg query to a txt file and then importing the hklm line with
(set /p rukey= )<profilerebuilder.txt

The problem is that the 1st line of the text file is blank and so returns a blank variable. Is there a way to have the set /p command pull the second line of the file or set multiple variables (1 per line?)


Report •

#5
February 13, 2013 at 18:42:22

I experimented with this:
@echo off
reg query "HKLM\SOFTWARE\MICROSOFT\WINDOWS NT\CURRENTVERSION\PROFILELIST" /s | findstr /i "%username%" > rr
cls
:: delims is TAB, not spaces in foll. line
for /f "tokens=3* delims=	" %%a in (rr) do call :xx "%%a" "%%b"
goto :eof

:xx
echo %~1
pause

Not sure if it will help, just tossed it into the grinder...

Report •

#6
February 14, 2013 at 10:16:55

Thanks for the response nbrane.

When I run that script as a test it seems to just break. It generates the rr file but just goes straight to closing after that, it doesn't actually echo anything or pause.

Seems to be that it's not calling the :xx properly.

When examining the rr file also, it appears that it's returning
ProfileImagePath REG_EXPAND_SZ C:\Users\%username%

I'm not sure if there's a way to actually get just the key and set that as the variable. I don't actually want/need the values for said key returned, just the key itself. Is there a way to specify that with reg query? maybe the break is in the reg query command and not the for command.


Report •

#7
February 14, 2013 at 19:01:25

Thanks for the suggestions guys. I did get it to work and parse out just the data that I want. the findstr idea is what I was initially overlooking as I was struggling to get it to select only the line I wanted.

The batch as I have it now (working) is:

@echo off
cls

:startreg
cls

set regq= reg query "hklm\software\microsoft\windows nt\currentversion\profilelist" /s /f %curuser%

for /f "tokens=* delims=~" %%A IN ('%regq% ^| findstr /i "microsoft"') DO set regkey=%%A

echo if you see the key below then the command worked.
echo %regkey%

pause
exit

It is now setting the registry key as the variable which is what I was looking to do. Thank you again for the input guys, it helped a ton.


Report •

#8
February 14, 2013 at 19:36:15

I got rid of the TAB requirement, which was unnecessary (too many beers). I figure that was messing things up. Here's the beta release:
@echo off
reg query "HKLM\SOFTWARE\MICROSOFT\WINDOWS NT\CURRENTVERSION\PROFILELIST" /s | findstr /i "%username%" > rr
cls
for /f "tokens=2*" %%a in (rr) do call :xx "%%b"
echo target aquired? %target%
goto :eof

:xx
echo %~2
set target=%~2
pause

edit: oops! crossed in the mail. ;-)


Report •


Ask Question