Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

What is the best way to find a user's Documents folder on XP and Vista from a batch script? Is it safe to assume that it's %USERPROFILE%\Documents?

share|improve this question

8 Answers 8

A complete reference of environment variables can be found here, on the microsoft site, it can also be found in a registry key.

HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders
String value: Personal

In the event the My Documents folder is not in the standard location, pulling the information out the registry key is probably the most reliable way.

share|improve this answer
2  
Thanks! Here's the registry query command for this value: reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "Personal" –  lajos May 21 '09 at 23:25

so my final version looks like this:

FOR /F "tokens=3 delims= " %%G IN ('REG QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "Personal"') DO (SET docsdir=%%G)

where the character between delims= and the following " is a single tab. Make sure your editor emits a tab and not spaces.

EDIT: On Windows 7 (and maybe all windows) you shouldn't specify delims= at all as it defaults to which is the whitespace used inbetween the tokens and not just a tab.

share|improve this answer
    
Removing the delims= works for me on Windows 8 –  Andrew Arnott 7 hours ago

not safe for xp, there it is my documents and it is localizable.

share|improve this answer

That'll be the place that the system knows about (although it's My Documents in XP), however you'll have to judge for yourself whether or not that's a safe assumption for your environment. If you use My Documents redirection, though, that variable should be set to whatever you redirect to with the default set to c:\documents and settings[username] in XP or c:\users[username] in Vista.

share|improve this answer
    
@squillman: I need to work on my typing speed! You smoked me on this answer! lol –  JFV May 21 '09 at 19:15
    
Proof that time spent in chat rooms during college was worth it... :) –  squillman May 21 '09 at 19:16

In Vista it's %UserProfile%\Documents, but in WinXP it's %UserProfile%\My Documents.

That's the only difference between the 2 different OS's. And don't forget that the Videos, Music, and Photos are under C:\Users[username] in Vista too.

-JFV

share|improve this answer

Windows XP names it 'My Documents' and Vista names it 'Documents'. You might put in a script to determine which OS you are running.

@echo off
IF EXIST "%USERPROFILE%\My Documents" (
    echo Windows XP
) ELSE (
    echo Vista
)
share|improve this answer

It's only "My Documents" etc on english windows. If you're using another language the pathname is "translated" (except on Vista)

share|improve this answer

:getuserdoc folder from registry

set idkey="HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders"

call:fetchvalue errorlevelvariable %idkey% Personal

goto:eof

errorlevel is set if fail

:fetchvalue

set /a %1=0

REG query %2 /v %3

if ERRORLEVEL 1 (set /a %1=1&goto :eof)

FOR /F "tokens=3* skip=2 delims= " %%A IN ('REG QUERY %2 /v %3') DO SET %3="%%A"

goto:eof

EDIT Use Button GETSource as this wenpage cant display answer characters are removed!

share|improve this answer
    
This looks like you want it to be a script? Please read our markdown editing/formatting guide and fix up your formatting. You may also want to add some description of what your script does and when/why you should/shouldn't use it. –  voretaq7 Dec 16 '12 at 18:40

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.