Finding user’s documents folder in .bat script

batch-filewindows-vistawindows-xp

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?

Best Answer

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.