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?
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?
so my final version looks like this:
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.
A complete reference of environment variables can be found here, on the microsoft site, it can also be found in a registry key.
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.
It's only "My Documents" etc on english windows. If you're using another language the pathname is "translated" (except on Vista)
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:
Here is the one-liner for script writers:
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:
http://ss64.com/nt/for_f.html
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.)
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.
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.
In Windows Vista, it is
but in Windows XP, it is
That's the only difference between the two different operating systems.
And don't forget that in Windows Vista the
Videos
,Music
, andPhotos
directories are all underC:\Users\[%USERNAME]
Use Button GETSource as this wenpage cant display answer characters are removed!
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: