views:

46

answers:

3

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.

+1  A: 

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.

EDIT: The path to that folder is stored in "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" under "Personal". You need registry access though.

Source: Microsoft

Excellent suggestion about that registry key.
Onorio Catenacci
+1  A: 

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

Kragen
+1  A: 
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.

Moisei