Is There A System Defined Environment Variable For Documents Directory?

0 votes
asked Aug 16, 2010 by onorio-catenacci

I know about the %USERPROFILE% system defined environment variable on Windows XP (and Vista and Windows 7). Is there a system defined environment variable pointing to the location of the "My Documents" directory? On XP by default it's %USERPROFILE%\My Documents and on Win 7 it's %USERPROFILE%\Documents. I just wanted to avoid having to test for the OS version in a Powershell script if I can avoid it.

15 Answers

0 votes
answered Aug 16, 2010 by msteiger

On my default-installation XP system, there is no environment variable for that. You can list all variables with the "set" command ( no parameters ) in the command line. So probably you have to do a test.

If you don't want to test for the OS version, you can simply check whether "Documents" exists and if not then try "My Documents" or vice versa. This isn't perfect however, because s/o could have a "Documents" folder on his XP machine.

Btw: my system is German, so the folder is called "Dokumente". You might need to take that into account.

The path to that folder is stored in

HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders

under Personal. You need registry access, though.

Source: Microsoft

0 votes
answered Aug 16, 2010 by moisei
C:\Documents and Settings\mrabinovitch>set | grep -i document
ALLUSERSPROFILE=C:\Documents and Settings\All Users
APPDATA=C:\Documents and Settings\myuser\Application Data
HOMEPATH=\Documents and Settings\myuser
USERPROFILE=C:\Documents and Settings\myuser

as you can see there is no such a vairable.

0 votes
answered Aug 16, 2010 by justin

If you type:

set

In a command prompt you will get a list of all environment variables defined on your system.

Looking at the ones defined on mine (Windows 7 Home Premium) none of them appear to point towards My Documents.

FYI:

The SHGetSpecialFolderPath function can be used to get the path to the My Documents directory. Alternatively the Environment.GetFolderPath method can be used under .Net

0 votes
answered Aug 4, 2012 by john-chacho

Some confusion may be due to the availability of CSIDL/KNOWNFOLDERID values vs command shell environment variables.

0 votes
answered Aug 18, 2012 by flindeberg

For .NET the following holds true (ie not applicable in all windows applications):

As one answer points out, there is no Environment Variable pointing to My Documents but there is Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) (C#) for .NET.

I'm adding this answer since this question comes up when googling for C#, environment variables and my documents and Justin's answer does not contain the line of code :)

Using the above mentioned line of code is the preferred way of accessing my documents in .NET :)

Copy paste this row for C# usage:

var directoryNameOfMyDocuments = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

Note that C# needs a capital D in MyDocuments.

0 votes
answered Aug 30, 2012 by hipponax43

Extending flindeberg's answer to PowerShell becomes:

PS> [Environment]::GetFolderPath("mydocuments")
0 votes
answered Aug 2, 2013 by nick-grealy

(Just to reiterate the previous answers) There is no environment variable provided out-of-the-box (WinXP) for the "My Documents" directory.

However, you can set a variable, with the following command:

Tested on Windows 7 / 8.1:

for /f "tokens=3* delims= " %a ^
in ('reg query "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "Personal"') ^
do (set mydocuments=%a %b)

or (one liner)

for /f "tokens=3* delims= " %a in ('reg query "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "Personal"') do (set mydocuments=%a %b)

Which would then give you a %mydocuments% variable:

C:\>echo mydocuments="%mydocuments%"

mydocuments="C:\pathto\My Documents"

(Does anyone use XP/Vista? If yes, can test this and let us know if it works?)

0 votes
answered Aug 8, 2013 by user3080328

Actually, the %USERPROFILE%\My Documents should work in Windows 7. It's what I use.

0 votes
answered Aug 15, 2014 by neal-bangia

There does not exist by design a documents environment variable in windows. You have to create a customized one. Do this by going here. Define an environment variable called MYDOCUMENTS to reference whichever location you need referenced. Thereafter, it shall be an environment variable that you reference by %MYDOCUMENTS%.

0 votes
answered Aug 9, 2015 by atif-aziz

In addition to answers based on registry, .NET and PowerShell, you could also use WshSpecialFolders from WSH. Here's a self-contained command/batch script demonstrating how:

@echo off
call :script > "%temp%\%~n0.js" && cscript //nologo "%temp%\%~n0.js" %*
goto :EOF

:script
echo var specialFolders = WScript.CreateObject('WScript.Shell').SpecialFolders;
echo if (WScript.Arguments.length === 0) {
echo     for (var e = new Enumerator(specialFolders); !e.atEnd(); e.moveNext()) {
echo         WScript.Echo(e.item());
echo     }
echo } else {
echo     for (var e = new Enumerator(WScript.Arguments); !e.atEnd(); e.moveNext()) {
echo         WScript.Echo(specialFolders(e.item()));
echo     }
echo }
goto :EOF

It emits a WSH script in JScript and uses it to get one or more paths for special folder tokens supplied as arguments. Assuming you save the above script as a file called specialf.cmd, the usage for getting path to current user's documents directory would be:

specialf MyDocuments

Here's another usage testing all special folder tokens:

specialf ^
  AllUsersDesktop ^
  AllUsersStartMenu ^
  AllUsersPrograms ^
  AllUsersStartup ^
  Desktop ^
  Favorites ^
  Fonts ^
  MyDocuments ^
  NetHood ^
  PrintHood ^
  Programs ^
  Recent ^
  SendTo ^
  StartMenu ^
  Startup ^
  Templates

You could use this to capture into an environment variable like this:

for /f "delims=/" %p in ('specialf MyDocuments') do @set MYDOCS=%p
Welcome to Q&A, where you can ask questions and receive answers from other members of the community.
Website Online Counter

...