Is There A System Defined Environment Variable For Documents Directory?
I know about the %USERPROFILE%
sys开发者_如何学Ctem 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.
For powershell the following works:
[environment]::getfolderpath("mydocuments")
and avoiding magic strings
[Environment]::GetFolderPath([Environment+SpecialFolder]::MyDocuments)
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.
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
There's no inbuilt enviornment variable, but in PowerShell you can find the location with:
[Environment]::GetFolderPath("mydocuments")
You can also obviously create an environment variable with:
$env:DOCUMENTS = [Environment]::GetFolderPath("mydocuments")
(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?)
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
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.
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
Some confusion may be due to the availability of CSIDL/KNOWNFOLDERID values vs command shell environment variables.
- Recognized CSIDL values from Microsoft
- KNOWNFOLDERID from Microsoft
For a batch file in Windows 7 (at least), Nick G's solution needs a slight tweak to set the user-defined variable UserDocuments :
FOR /F "tokens=3* delims= " %%a in ('reg query "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "Personal"') do (set UserDocuments=%%a)
Note the only differences are,
- Use only one space character for delims
- %%a instead of %a
To avoid seeing the line, but to see the results, use :
@FOR /F "tokens=3* delims= " %%a in ('reg query "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "Personal"') do @(Set UserDocuments=%%a)
@Echo ~~~~~~~~ UserDocuments=%UserDocuments%
Thanks Nick G. Your answer taught me a lot. I hope this helps someone else.
Improved @NickGrealy answer:
reg query
outputs
empty_line reg_key_path name type value
- there can be an arbitrary amount of 'space chars' between words in a registry value, and the
%a %b
string is not correct in this case
So, using the skip=2
option to skip first lines and the tokens=2*
option to pass a registry value to the %b
var:
for /f "skip=2 tokens=2*" %A in ('reg query "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "Personal"') do @set "UserDocs=%B"
or for script files:
for /f "skip=2 tokens=2*" %%A in ('reg query "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "Personal"') do set "UserDocs=%%B"
But taking into account the registry value [HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\!Do not use this registry key]
Based on @AtifAziz answer:
for /f "tokens=*" %A in ('echo WScript.Echo^(^(new ActiveXObject^("WScript.Shell"^)^).SpecialFolders^("MyDocuments"^)^)^>%TEMP%\getdoc.js ^& cscript /nologo %TEMP%\getdoc.js ^& del /q %TEMP%\getdoc.js') do @set "UserDocs=%A"
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%.
Tested and worrking in win XP, vista, 8, 8.1 and 10!!
@echo off
for /f "skip=2 tokens=2*" %%c in ('reg query "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "Personal"') do @set "docs=%%d" && echo WIN XP - 10
xcopy "C:\test.txt" "%docs%" /f /y
echo %docs%
pause
EXIT
Windows Batch File (.bat) or Windows Command Script (.cmd)
@echo off
ver | find "XP" > nul
if %ERRORLEVEL% == 0 set Docs=%UserProfile%\My Documents & echo WIN XP
if %ERRORLEVEL% == 1 set Docs=%UserProfile%\Documents & echo WIN vista - 10
xcopy "C:\test.txt" "%Docs%" /f /y
pause
EXIT
update Windows Batch File (.bat) or Windows Command Script (.cmd)
@echo off
ver | find "XP" > nul
if %ERRORLEVEL% == 0 SET DOCS=%USERPROFILE%\My Documents & echo WIN XP
if %ERRORLEVEL% == 1 FOR /f "tokens=3" %%x IN ('REG QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "Personal"') do (set docs=%%x) & echo WIN vista - 10
xcopy "C:\test.txt" "%docs%" /f /y
echo %docs%
pause
EXIT
Here's the full list of user variables for all users running Windows.
The reg query portion of the code will find that value and set it so it can be used immediately and allows the other codes to set it permanently for all users. The ad registry portion of the code will enable it for all users. The setx makes it so you don't have to log off and log back in to be able to use the codes.
The current user variables only applies to the person who installed this code.
@ECHO OFF
ECHO REG ALL USER variables
ECHO all user desktop
for /f "skip=2 tokens=3*" %%c in ('reg query "HKLM\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "Common Desktop"') do @set "ALLDT=%%d"
REG ADD "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v "ALLDT" /t "REG_SZ" /d "%ALLDT%" /f
setx ALLDT "%ALLDT%"
Echo all user's documents
for /f "skip=2 tokens=3*" %%c in ('reg query "HKLM\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "Common Documents"') do @set "ALLDOC=%%d"
REG ADD "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v "ALLDOC" /t "REG_SZ" /d "%ALLDOC%" /f
setx ALLDOC "%ALLDOC%"
echo all user start menu
for /f "skip=2 tokens=3*" %%c in ('reg query "HKLM\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "Common Programs"') do @set "ALLSM=%%d"
REG ADD "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v "ALLSM" /t "REG_SZ" /d "%ALLSM%" /f
setx ALLSM "%ALLSM%"
Echo all users startup
for /f "skip=2 tokens=3*" %%c in ('reg query "HKLM\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "Common Startup"') do @set "ALLSMSTU=%%d"
REG ADD "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v "ALLSMSTU" /t "REG_SZ" /d "%ALLSMSTU%" /f
setx ALLSMSTU "%ALLSMSTU%"
Echo all users music
for /f "skip=2 tokens=2*" %%c in ('reg query "HKLM\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "CommonMusic"') do @set "ALLMU=%%d"
REG ADD "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v "ALLMU" /t "REG_SZ" /d "%ALLMU%" /f
setx ALLMU "%ALLMU%"
echo all users pictures
for /f "skip=2 tokens=2*" %%c in ('reg query "HKLM\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "CommonPictures"') do @set "ALLPIC=%%d"
REG ADD "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v "ALLPIC" /t "REG_SZ" /d "%ALLPIC%" /f
setx ALLPIC "%ALLPIC%"
Echo all users videos
for /f "skip=2 tokens=2*" %%c in ('reg query "HKLM\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "CommonVideo"') do @set "ALLVID=%%d"
REG ADD "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" /v "ALLVID" /t "REG_SZ" /d "%ALLVID%" /f
setx ALLVID "%ALLVID%"
Echo set cerrent user variables
Echo current users desktop
for /f "skip=2 tokens=2*" %%c in ('reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "Desktop"') do @set "myDesktop=%%d"
REG ADD "HKEY_CURRENT_USER\Environment" /v "myDesktop" /t "REG_SZ" /d "%myDesktop%" /f
setx myDesktop "%myDesktop%"
Echo current users documents
for /f "skip=2 tokens=2*" %%c in ('reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "Personal"') do @set "mydoc=%%d"
REG ADD "HKEY_CURRENT_USER\Environment" /v "mydoc" /t "REG_SZ" /d "%mydoc%" /f
setx mydoc "%mydoc%"
Echo current user start menu
for /f "skip=2 tokens=3*" %%c in ('reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "Start Menu"') do @set "myStart=%%d"
REG ADD "HKEY_CURRENT_USER\Environment" /v "myStart" /t "REG_SZ" /d "%myStart%" /f
setx myStart "%myStart%"
Echo current user startup
for /f "skip=2 tokens=2*" %%c in ('reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "Startup"') do @set "myStartup=%%d"
REG ADD "HKEY_CURRENT_USER\Environment" /v "myStartup" /t "REG_SZ" /d "%myStartup%" /f
setx myStartup "%myStartup%"
Echo current users music
for /f "skip=2 tokens=3*" %%c in ('reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "My Music"') do @set "myMU=%%d"
REG ADD "HKEY_CURRENT_USER\Environment" /v "myMU" /t "REG_SZ" /d "%myMU%" /f
setx myMU "%myMU%"
Echo current users pictures
for /f "skip=2 tokens=3*" %%c in ('reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "My Pictures"') do @set "myPIC=%%d"
REG ADD "HKEY_CURRENT_USER\Environment" /v "myPIC" /t "REG_SZ" /d "%myPIC%" /f
setx myPIC "%myPIC%"
Echo current users video
for /f "skip=2 tokens=3*" %%c in ('reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders" /v "My Video"') do @set "myVID=%%d"
REG ADD "HKEY_CURRENT_USER\Environment" /v "myVID" /t "REG_SZ" /d "%myVID%" /f
setx myVID "%myVID%"
exit
Actually, the %USERPROFILE%\My Documents
should work in Windows 7. It's what I use.
精彩评论