Link to home
Start Free TrialLog in
Avatar of dscits
dscits

asked on

Retrieve "My Documents" location from registry and assign variable

I'm trying to retrieve the location of My Documents from the Registry and assign it a variable in a batch file.

With this command I can retrieve the location, but it also has extra text at the beginning:

reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\ShellFolders" /v personal

Open in new window


HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders
    personal    REG_SZ    C:\Users\User\Documents

Open in new window


I need the variable to be only the path of the folder itself, without any other text.
Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

Try this using a for command to read the second token into %%a and the rest of the line into %%b and assign to a variable:

@echo off
for /f "tokens=2* delims= " %%a in ('reg query

"HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v personal') do set

MyDocs="%%b"
echo My Documnts points to %MyDocs%
ASKER CERTIFIED SOLUTION
Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Bizarre, they've clearly done something with how it works on EE now on this old IE6 machine, it looked OK before submitting!
Right, one last try pasted into Notepad and not typed straight in this time!
@echo off
for /f "tokens=2* delims= " %%a in ('reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v personal') do set MyDocs="%%b"
echo My Documnts points to %MyDocs%

Open in new window

Avatar of dscits
dscits

ASKER

That works perfectly when all the code is on one line.  Thanks!
No problem, sorry it took a while for the simple bits, will clearly have to type in notepad and paste if I use this machine another time!
Avatar of dscits

ASKER

huh looks like that only works if there are no spaces in the path.
Should be OK, just tried it as posted in 34447996 and there are spaces in the path i.e. c:\documents and settings... " etc.

What effect are you getting?  Please post output from

reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v personal

for that machine.

Steve
Having turned echo off this is what I get:

C:\>rem @echo off

C:\>for /F "tokens=2* delims= " %a in ('reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Ex
plorer\Shell Folders" /v personal') do set MyDocs="%b"

C:\>set MyDocs=""

C:\>set MyDocs="C:\Documents and Settings\Administrator\My Documents"

C:\>echo My Documnts points to "C:\Documents and Settings\Administrator\My Documents"
My Documnts points to "C:\Documents and Settings\Administrator\My Documents"
Avatar of dscits

ASKER

When I use this batch file:

FOR /f "tokens=2* delims= " %%a IN ('reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v personal') DO SET MYDOCS="%%b"

ECHO My Documents Location is:  %MYDOCS%

Open in new window


I get:

My Documents Location is:  "Settings\Administrator\My Documents"

Open in new window

Avatar of dscits

ASKER

I should add I am testing this in a Win XP SP3 VM via VMWare.  Works fine in Win7 host OS.
Avatar of dscits

ASKER

Output from reg query:

C:\Documents and Settings\Administrator>reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v personal

! REG.EXE VERSION 3.0

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders
    personal    REG_SZ  C:\Documents and Settings\Administrator\My Documents

Open in new window

OK what is the output of the req query then... I only had 2003 server around to test as this machine is 2000 which doesn't have REG.EXE.  Change it to 1* in the tokens then by the sounds of it, or post the output of the req query command and will look what it should be.  Just tried it on another XP machine and got the same output from the reg.exe commnd as with 2003 server though?
Avatar of dscits

ASKER

When tokens is changed to 1* I get:

My Documents Location is:  "and Settings\Administrator\My Documents"

Open in new window


almost there, just one more word!
Hmm, somethin odd here.  You can remove the 1* and replace it witj just * and then change the %%b in the set command to an %%a BUT I'm no sure what you are getting here.  When I run it on each machine here it returns he same output of you and the correct path and the for command effectively is asking it to split the path at the spaces.  Can you run it without the @echo on line please too?

Avatar of dscits

ASKER

@echo off

reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v personal

pause

Open in new window


Results in:

! REG.EXE VERSION 3.0

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders
    personal    REG_SZ  C:\Documents and Settings\Administrator\My Documents

Press any key to continue . . .

Open in new window


is that what you needed?
Avatar of dscits

ASKER

Could it be that some virtual machines aren't reliable when using this code?

Works fine with 2* on a Windows Vista VM.

This XP VM is pretty much stock.  Just Security Essentials installed, not much else.
Avatar of dscits

ASKER

Is it possible to copy the whole line to a variable (with just delims *) and then use findstr or something similar to remove the first portion of "    personal     REG_SZ   " ?
out and about. Will look what is going on when i retyurn.

Steve
Avatar of dscits

ASKER

hey Steve any workarounds that you can think of?
Sorry have been busy.  Will look again.  I wonder if the output is showing tabs instead of spaces on some OS sub-versions?  Ttry using notepad to edit the script, add the lines delims= X just before the " and after tokens=2* i.e. "tokens=2* delims= X" where X is the tab character.

If not will look at other options - I tried on an XP SP2, 2003 server machines before.
Avatar of dscits

ASKER

That did it!  Unfortunately it breaks on Windows 7 now, but I can change the script according to the output of my system info utility OS name.

thanks a lot!
if you have both space and tab in the delims it should work on either then.... I wonder why it worked on my xp machine?!
Avatar of dscits

ASKER

Yes, if I put the Tab first, and then space it does work on both now:

@echo off

FOR /f "tokens=2* delims=	 " %%a IN ('reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v personal') DO SET MYDOCS="%%b"

ECHO My Documents Location is:  %MYDOCS%

pause

Open in new window

Avatar of dscits

ASKER

I've tested it and it works on Windows 2000 (Unofficial SP5 w/reg.exe), XP, Vista, and Server 2003.
Good, sorry it took a while.... Guess this is why we test things on different OS before rolling out to users :-)

SP5 for 2000?  really? Is that just one of the rollups of fixes etc. Tell me more if you dont mind, will google it I guess in a sec though - few stick in the mud clients still  on 2000 servers and clients hence why I run a machine here still (aswell as XP and 7 though Vista soon went the Windows ME route)