是否有系统定义的文档目录环境变量?

发布于 2024-09-15 00:29:52 字数 248 浏览 13 评论 0原文

我知道 Windows XP(以及 Vista 和 Windows 7)上的 %USERPROFILE% 系统定义的环境变量。是否有系统定义的环境变量指向“我的文档”目录的位置?在 XP 上默认为 %USERPROFILE%\My Documents,在 Win 7 上默认为 %USERPROFILE%\Documents。我只是想避免在 Powershell 脚本中测试操作系统版本(如果可以的话)。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(16

浴红衣 2024-09-22 00:29:52

对于 powershell,以下内容有效:

[environment]::getfolderpath("mydocuments")

并避免魔术字符串

[Environment]::GetFolderPath([Environment+SpecialFolder]::MyDocuments)

对于 .NET,以下内容成立(即不适用于所有 Windows 应用程序):

正如一个答案指出的那样,没有环境变量指向“我的文档”,但.NET 有 Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) (C#)。

我添加这个答案是因为在谷歌搜索 C#、环境变量和我的文档时出现这个问题,而 Justin 的答案不包含代码行:)

首选使用上述代码行在 .NET 中访问我的文档的方式:)

复制粘贴此行以供 C# 使用:

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

请注意,C# 在 MyDocuments 中需要大写 D。

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.

捎一片雪花 2024-09-22 00:29:52

在我的默认安装 XP 系统上,没有相应的环境变量。您可以在命令行中使用“set”命令(无参数)列出所有变量。所以可能你必须做一个测试。

如果您不想测试操作系统版本,您可以简单地检查“文档”是否存在,如果不存在,则尝试“我的文档”,反之亦然。然而这并不完美,因为 s/o 的 XP 计算机上可能有一个“Documents”文件夹。

顺便说一句:我的系统是德语,所以该文件夹名为“Dokumente”。您可能需要考虑到这一点。

该文件夹的路径存储在

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

Personal 下。不过,您需要注册表访问权限。

来源:微软

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

ぇ气 2024-09-22 00:29:52

没有内置的环境变量,但在 PowerShell 中您可以使用以下命令找到该位置:

[Environment]::GetFolderPath("mydocuments")

您显然还可以使用以下命令创建环境变量:

$env:DOCUMENTS = [Environment]::GetFolderPath("mydocuments")

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")
西瑶 2024-09-22 00:29:52

(只是为了重申前面的答案)没有为“我的文档”目录提供开箱即用(WinXP)的环境变量。

但是,您可以使用以下命令设置一个变量:

在 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)

或(一行)

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)

这将为您提供一个 %mydocuments% 变量:(

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

mydocuments="C:\pathto\My Documents"

有人使用 XP/Vista 吗? ?如果是,可以测试一下并让我们知道它是否有效?)

(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?)

回眸一笑 2024-09-22 00:29:52

如果您键入:

set

在命令提示符中,您将获得系统上定义的所有环境变量的列表。

看看我的(Windows 7 Home Premium)上定义的那些,它们似乎都没有指向“我的文档”。

仅供参考:

SHGetSpecialFolderPath 函数可用于获取我的文档目录的路径。或者,可以在 .Net 下使用 Environment.GetFolderPath 方法

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

鼻尖触碰 2024-09-22 00:29:52
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

正如你所看到的,不存在这样的变量。

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.

静若繁花 2024-09-22 00:29:52

除了基于注册表的答案之外,.NETPowerShell,您还可以使用.NETPowerShell microsoft.com/en-us/library/9x9e7edx(v=vs.84).aspx" rel="nofollow noreferrer">WshSpecialFolders 来自 WSH。这是一个自包含命令/批处理脚本,演示了如何操作:

@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

它在 JScript 中发出 WSH 脚本,并使用它来获取作为参数提供的特殊文件夹标记的一个或多个路径。假设您将上述脚本保存为名为 specialf.cmd 的文件,获取当前用户文档目录路径的用法将是:

specialf MyDocuments

这是测试所有特殊文件夹令牌的另一种用法:

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

您可以使用它来捕获像这样的环境变量:

for /f "delims=/" %p in ('specialf MyDocuments') do @set MYDOCS=%p

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
噩梦成真你也成魔 2024-09-22 00:29:52

一些混乱可能是由于 CSIDL/KNOWNFOLDERID 值与命令 shell 环境变量的可用性造成的。

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

野鹿林 2024-09-22 00:29:52

对于 Windows 7 中的批处理文件(至少),Nick G 的解决方案需要稍作调整来设置用户定义的变量 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)

请注意,唯一的区别是,

  1. 仅使用一个空格字符进行分隔符
  2. %%a 而不是 %a 以

避免看到这条线,但要查看结果,请使用:

@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%

谢谢 Nick G。你的回答教会了我很多。我希望这对其他人有帮助。

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,

  1. Use only one space character for delims
  2. %%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.

小忆控 2024-09-22 00:29:52

改进了 @NickGrealy 答案:

  1. reg query 输出
empty_line
reg_key_path
name  type  value
  1. 注册表值中的单词之间可以有任意数量的“空格字符”,并且 %a %b 字符串不正确在本例中

,因此,使用 skip=2 选项跳过第一行,并使用 tokens=2* 选项将注册表值传递给 %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"

或对于脚本文件:

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"

但考虑到注册表值[HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\!请勿使用此注册表项]

基于@阿蒂夫·阿齐兹 回答:

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"

Improved @NickGrealy answer:

  1. reg query outputs
empty_line
reg_key_path
name  type  value
  1. 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"
心在旅行 2024-09-22 00:29:52

Windows 中设计时不存在文档环境变量。您必须创建一个定制的。请前往此处执行此操作。定义一个名为 MYDOCUMENTS 的环境变量来引用您需要引用的任何位置。此后,它应该是您通过 %MYDOCUMENTS% 引用的环境变量。

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%.

秋日私语 2024-09-22 00:29:52

已在 win XP、vista、8、8.1 和 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

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
柒七 2024-09-22 00:29:52

Windows 批处理文件 (.bat) 或 Windows 命令脚本 (.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

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
灼痛 2024-09-22 00:29:52

更新 Windows 批处理文件 (.bat) 或 Windows 命令脚本 (.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

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
晒暮凉 2024-09-22 00:29:52

以下是运行 Windows 的所有用户的用户变量的完整列表。

代码的 reg 查询部分将找到该值并对其进行设置,以便可以立即使用它,并允许其他代码为所有用户永久设置它。
代码的广告注册部分将为所有用户启用它。 setx 使您无需注销并重新登录即可使用代码。

当前用户变量仅适用于安装此代码的人。

@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

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
暮年 2024-09-22 00:29:52

实际上,%USERPROFILE%\My Documents 应该可以在 Windows 7 中工作。这就是我使用的。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文