fly Posted April 26, 2007 Share Posted April 26, 2007 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=%%KThis 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 More sharing options...
[deXter] Posted April 26, 2007 Share Posted April 26, 2007 FOR /F "skip=4 tokens=2*" %%i in ('REG QUERY ^"HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders^" /v Personal') DO SET KEY=%%jThat REG.EXE output actually generates 7 lines, the first 4 of which should be ignored. Link to comment Share on other sites More sharing options...
fly Posted April 26, 2007 Author Share Posted April 26, 2007 Sweet Jesus. Thank you man. I don't understand why I was still getting the row output using only skip=2, but whatever. Link to comment Share on other sites More sharing options...
jaclaz Posted April 26, 2007 Share Posted April 26, 2007 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 offREM 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 TABFOR /F "tokens=3* delims= " %%K in ('REG QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\Personal"') DO ECHO %%Kjaclaz Link to comment Share on other sites More sharing options...
[deXter] Posted April 26, 2007 Share Posted April 26, 2007 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 More sharing options...
Yzöwl Posted April 26, 2007 Share Posted April 26, 2007 As noted in jaclaz' reply, the caret is not required as an escape character for the double quotes in side the reg command parantheses. Link to comment Share on other sites More sharing options...
fly Posted April 26, 2007 Author Share Posted April 26, 2007 Okay one more question with this script...Here it is in its entirety:::@ECHO OFF:: Find where My Docs has been redirected toFOR /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 existMKDIR "%KEY%\Bluezone\"MKDIR "%KEY%\Bluezone\Config\":: Copy local .zmd files up to redirected store, if they dont existFOR %%K in ("%USERPROFILE%\My Documents\Bluezone\Config\*.zmd") DO ( IF NOT EXIST "%KEY%\Bluezone\Config\%%~nxK" XCOPY "%%K" "%KEY%\Bluezone\Config\")PAUSEFor some reason, it seems to be taking %USERPROFILE% from %KEY% literally and not as a variable. Why? Link to comment Share on other sites More sharing options...
jaclaz Posted April 26, 2007 Share Posted April 26, 2007 I re-checked, the token appears to be 3*, not 2* , however it still does not work with /v syntax @dexteryes, you are right, the line without "delims= " works allright. Can anyone try running this?:@echo offCLSSET KEY=:: delims is a TABFOR /F "tokens=3* delims= " %%K in ('REG QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders\Personal"') DO SET KEY=%%KECHO 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=%%KECHO 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=%%KECHO 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=%%KECHO 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=%%LECHO KEY5 IS %KEY%ECHO.What I get is the following:KEY1 IS %USERPROFILE%\DocumentiKEY2 IS HistoryKEY3 IS %USERPROFILE%\DocumentiKEY4 IS %USERPROFILE%\ImpostazioniKEY5 IS %USERPROFILE%\Documentii.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 More sharing options...
jaclaz Posted April 26, 2007 Share Posted April 26, 2007 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 More sharing options...
fly Posted April 26, 2007 Author Share Posted April 26, 2007 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%jaclazInteresting. I can't say that I totally understand why that is, but thanks! Link to comment Share on other sites More sharing options...
Yzöwl Posted April 26, 2007 Share Posted April 26, 2007 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 strMyDocsConst MY_DOCS = &H5&Set objShell = CreateObject("Shell.Application")Set objFolder = objShell.Namespace(MY_DOCS)Set objFolderItem = objFolder.SelfWscript.Echo objFolderItem.Path Link to comment Share on other sites More sharing options...
jftuga Posted April 30, 2007 Share Posted April 30, 2007 What does the %%? do? I am used to seeing letters, like %%i for example.Thanks,-John Link to comment Share on other sites More sharing options...
jaclaz Posted April 30, 2007 Share Posted April 30, 2007 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 More sharing options...
jftuga Posted April 30, 2007 Share Posted April 30, 2007 Ok, I thought it may have referred to the last token., esp when/if the number of tokens was inconsistent.-John Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now