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?

10 Answers 10


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.

  • 2
    Removing the delims= works for me on Windows 8 Oct 30, 2014 at 16:48

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.

  • 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, 2009 at 23:25

The best way to determine the location of My Documents is from the Windows Registry.

Several other answers and comments on this page have made reference to using "reg query". Below is the correct implementation that takes into account spaces in the path, as well as different versions of Windows:

for /f "tokens=1,2*" %%A in ('reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "Personal" 2^>nul') do (
   set RNAME=%%A
   set RTYPE=%%B
   set RDATA=%%C

Here is the one-liner for script writers:

for /f "tokens=1,2*" %%A in ('reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "Personal" 2^>nul') do set MY_DOCS_ROOT=%%C

This does not take into account localization or internationalization. This has not been tested on non-English versions of Windows. Comments on that topic are welcome.

This does work for Windows XP, Windows Vista, Windows 7, and Windows 8.

Note: Using the asterisk in the tokens= option is important for Windows XP, which usually contains spaces in the path for My Documents.

Note: If using implicit variables like %%B and %%C seems a little strange, you may have a look at this article:


tokens=3* will process the third token and the 4th + all subsequent items, this can also be written as tokens=3,*

Each token specified will cause a corresponding parameter letter to be allocated. The letters used for tokens are case sensitive.

If the last character in the tokens= string is an asterisk, then additional parameters are allocated for all the remaining text on the line.

The first variable is declared in the FOR statement and subsequent variables will be implicitly declared via the tokens= option.

The linked article gives the exact ordering of the variables that will be declared implicitly, but it is essentially alphabetic.

(With three tokens, by declaring %%A in the FOR statement, %%B and %%C will be declared implicitly. In the same way, by declaring %%X in the FOR statement, %%Y and %%Z will be declared implicitly.)

  • Just note to use the 1-liner variable in subsequent calls refer to it like this (for .bat newbs): %MY_DOCS_ROOT%
    – CSchwarz
    Feb 6 at 0:04

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


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.

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

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


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
    echo Windows XP
) ELSE (
    echo Vista
:getuserdoc folder from registry
set idkey="HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders"
call:fetchvalue errorlevelvariable %idkey% Personal
errorlevel is set if fail
 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"

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

  • 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, 2012 at 18:40

For all who, like me, stumble upon this post while searching for a way to simply get the users documents folder, having next to zero experience about batch files, but want to use the great solution by Werkkrew or (probably?) the derived version by user55644, that's how I made it working on my windows 7 Pc:

echo off

setlocal enableextensions enabledelayedexpansion

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

echo %targetdir%

In Windows Vista, it is


but in Windows XP, it is

%USERPROFILE%\My Documents

That's the only difference between the two different operating systems.

And don't forget that in Windows Vista the Videos, Music, and Photos directories are all under C:\Users\[%USERNAME]

  • This did not work for me because the Documents folder is redirected in my profile. The registry way works in that case.
    – drescherjm
    Dec 21, 2017 at 18:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .