Jump to content

Need help pulling a registry value from a batch file


Recommended Posts

Okay, I'm trying to pull a registry key value into a variable and am having issues. FOR statements always mess me up...

Here is what I have so far.

FOR /F "skip=2 delims=~" %%K in ('REG QUERY ^"HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders^" /v Personal') DO SET KEY=%%K

This should work on every 2k/xp machine so feel free to please run it.

What I'm trying to do is take the 3rd part of %KEY% (which should be %USERPROFILE%\My Documents), but I can't get it to work. And yes, this MUST be done in batch. Thanks for any help.

Link to comment
Share on other sites


FOR /F "skip=4 tokens=2*" %%i in ('REG QUERY ^"HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders^" /v Personal') DO SET KEY=%%j

That REG.EXE output actually generates 7 lines, the first 4 of which should be ignored.

Link to comment
Share on other sites

hmmm, just for the record, REG.EXE is not "default" on Win2K, you have to install it expressly.

However, on my Win2K the /v syntax appears not to be working, this one does:


@echo off
REM FOR /F "skip=2 delims=~" %%K in ('REG QUERY ^"HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders^" /v Personal') DO SET KEY=%%K
:: delims is a TAB
FOR /F "tokens=3* delims= " %%K in ('REG QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\Personal"') DO ECHO %%K

jaclaz

Link to comment
Share on other sites

Yes, the delims is a tab, but there's no need to specify it explicitly as the default delims set consists of space and tab.

FOR /?
...
delims=xxx - specifies a delimiter set. This replaces the
default delimiter set of space and tab.
...

Link to comment
Share on other sites

Okay one more question with this script...

Here it is in its entirety:

::@ECHO OFF

:: Find where My Docs has been redirected to
FOR /F "skip=4 tokens=2*" %%I in ('REG QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v Personal') DO SET KEY=%%J

:: Create Bluezone dir, if it doesnt exist
MKDIR "%KEY%\Bluezone\"
MKDIR "%KEY%\Bluezone\Config\"

:: Copy local .zmd files up to redirected store, if they dont exist
FOR %%K in ("%USERPROFILE%\My Documents\Bluezone\Config\*.zmd") DO (
IF NOT EXIST "%KEY%\Bluezone\Config\%%~nxK" XCOPY "%%K" "%KEY%\Bluezone\Config\"
)

PAUSE

For some reason, it seems to be taking %USERPROFILE% from %KEY% literally and not as a variable. Why?

Link to comment
Share on other sites

I re-checked, the token appears to be 3*, not 2* , however it still does not work with /v syntax :unsure:

@dexter

yes, you are right, the line without "delims= " works allright. :)

Can anyone try running this?:


@echo off
CLS
SET KEY=
:: delims is a TAB
FOR /F "tokens=3* delims= " %%K in ('REG QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\Personal"') DO SET KEY=%%K
ECHO KEY1 IS %KEY%
ECHO.

SET KEY=
FOR /F "skip=4 tokens=2*" %%K in ('REG QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v Personal') DO SET KEY=%%K
ECHO KEY2 IS %KEY%
ECHO.

SET KEY=
FOR /F "tokens=3*" %%K in ('REG QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\Personal"') DO SET KEY=%%K
ECHO KEY3 IS %KEY%
ECHO.

SET KEY=
FOR /F "skip=4 tokens=3*" %%K in ('REG QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v Personal') DO SET KEY=%%K
ECHO KEY4 IS %KEY%
ECHO.

SET KEY=
FOR /F "tokens=2,*" %%K in ('REG QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v Personal') DO IF %%K==Personal SET KEY=%%L
ECHO KEY5 IS %KEY%
ECHO.

What I get is the following:

KEY1 IS %USERPROFILE%\Documenti

KEY2 IS History

KEY3 IS %USERPROFILE%\Documenti

KEY4 IS %USERPROFILE%\Impostazioni

KEY5 IS %USERPROFILE%\Documenti

i.e. "odd" keys (using "my" and "dexter's" code) do work, while "even" ones, based on the /v syntax, do not, #2 gets, as said above the wrong token, #4 gets last item and NOT the right one.

jaclaz

Link to comment
Share on other sites

Sorry, fly, crossposting.

For some reason, it seems to be taking %USERPROFILE% from %KEY% literally and not as a variable. Why?

It is perfectly normal, the VALUE of "KEY" is %USERPROFILE%\My Documents\, you need to transform it before:

ECHO %KEY%
CALL SET KEY=%KEY%
ECHO %KEY%

jaclaz

Link to comment
Share on other sites

Sorry, fly, crossposting.
For some reason, it seems to be taking %USERPROFILE% from %KEY% literally and not as a variable. Why?

It is perfectly normal, the VALUE of "KEY" is %USERPROFILE%\My Documents\, you need to transform it before:

ECHO %KEY%
CALL SET KEY=%KEY%
ECHO %KEY%

jaclaz

Interesting. I can't say that I totally understand why that is, but thanks!

Link to comment
Share on other sites

Since you are not directly requiring an environment variable, use a more appropriate key insted!

@for /f "tokens=3 delims=	" %%? in ('reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v Personal^|find "REG_"') do @echo/%%?

All on one line!

You could even move to vbscript and use either of these two methods of getting the path:

set WshShell = WScript.CreateObject("WScript.Shell")
strMyDocs = WshShell.SpecialFolders("MyDocuments")
Wscript.Echo strMyDocs

Const MY_DOCS = &H5&
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(MY_DOCS)
Set objFolderItem = objFolder.Self
Wscript.Echo objFolderItem.Path

Link to comment
Share on other sites

What does the %%? do? I am used to seeing letters, like %%i for example.

Try to see a variable in a FOR loop INSTEAD of "two percentage signs followed by a letter", as "two percentage signs followed by an ASCII character that is not ALREADY a special character, NOT necessarily limited to range a-z and A-Z" and it should make much more sense.

Just for the record, I find out the problem I had before with the /v switch, the reg.exe version (optional) with Win2K does NOT support the switch, you need an XP or later version. :(

So, my "alterenate" syntax is valid with Win2K and the /v one with XP and later. :)

jaclaz

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...