computing
  • 0

Solved Batch File To Set Variable Registry Keys As Variables

  • 0

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?

Share

1 Answer

  1. 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

    • 0