I'm writing a Windows batch file and want to copy something to the desktop. I think I can use this:
我正在编写一个Windows批处理文件,想要将一些东西复制到桌面。我想我可以用这个:
%UserProfile%\Desktop\
% UserProfile % \电脑\
However, I'm thinking, that's probably only going to work on an English OS. Is there a way I can do this in a batch file that will work on any internationalized version?
但是,我在想,这可能只会在一个英语操作系统上运行。是否有一种方法可以在任何国际化版本的批处理文件中完成?
UPDATE
更新
I tried the following batch file:
我尝试了以下批处理文件:
REG QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v Desktop
FOR /F "usebackq tokens=3 skip=4" %%i in (`REG QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v Desktop`) DO SET DESKTOPDIR=%%i
FOR /F "usebackq delims=" %%i in (`ECHO %DESKTOPDIR%`) DO SET DESKTOPDIR=%%i
ECHO %DESKTOPDIR%
And got this output:
,这个输出:
S:\>REG QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v Desktop HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders Desktop REG_EXPAND_SZ %USERPROFILE%\Desktop S:\>FOR /F "usebackq tokens=3 skip=4" %i in (`REG QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folder s" /v Desktop`) DO SET DESKTOPDIR=%i S:\>FOR /F "usebackq delims=" %i in (`ECHO ECHO is on.`) DO SET DESKTOPDIR=%i S:\>SET DESKTOPDIR=ECHO is on. S:\>ECHO ECHO is on. ECHO is on.
15 个解决方案
#1
13
I found that the best solution is to use a vbscript together with the batch file.
我发现最好的解决方案是与批处理文件一起使用vbscript。
Here is the batch file:
这是批处理文件:
@ECHO OFF
FOR /F "usebackq delims=" %%i in (`cscript findDesktop.vbs`) DO SET DESKTOPDIR=%%i
ECHO %DESKTOPDIR%
Here is findDesktop.vbs file:
这是findDesktop。根据文件:
set WshShell = WScript.CreateObject("WScript.Shell")
strDesktop = WshShell.SpecialFolders("Desktop")
wscript.echo(strDesktop)
There may be other solutions but I personally find this one less hackish.
也许有其他的解决办法,但我个人觉得这是一个不那么陈腐的方法。
I tested this on an English PC and also a French PC - it seems to work (Windows XP).
我在一台英国个人电脑和一台法国电脑上测试了这款软件,它似乎很好用(Windows XP)。
HTH,
HTH,
Iulian Şerbănoiu
IulianŞerbănoiu
#2
49
To be safe, you should use the proper APIs in Powershell (or VBScript)
Using PowerShell:
为了安全起见,您应该使用Powershell(或VBScript)中的适当api:
[Environment]::GetFolderPath("Desktop")
Copy something using Powershell:
复制使用Powershell的东西:
Copy-Item $home\*.txt ([Environment]::GetFolderPath("Desktop"))
Here is a VBScript-example to get the desktop path:
下面是一个用于获取桌面路径的vbscript示例:
dim WSHShell, desktop, pathstring, objFSO
set objFSO=CreateObject("Scripting.FileSystemObject")
Set WSHshell = CreateObject("WScript.Shell")
desktop = WSHShell.SpecialFolders("Desktop")
pathstring = objFSO.GetAbsolutePathName(desktop)
WScript.Echo pathstring
#3
13
KB's answer to use [Environment]::GetFolderPath("Desktop")
is obviously the official Windows API for doing this.
KB使用[环境]::GetFolderPath(“桌面”)的答案显然是执行此操作的官方Windows API。
However, if you're working interactively at the prompt, or just want something that works on your machine, the tilda (~) character refers to the current user's home folder. So ~/desktop
is the user's desktop folder.
但是,如果您是在提示符之间交互工作,或者只是想要在您的机器上运行的东西,那么tilda(~)字符是指当前用户的主文件夹。所以~/桌面是用户的桌面文件夹。
#4
11
At least on Windows XP, Vista and 7 you can use the "%UserProfile%\Desktop"
safely.
至少在Windows XP、Vista和7上,你可以安全地使用“%UserProfile%\Desktop”。
Windows XP en-US it will expand to "C:\Documents and Settings\YourName\Desktop"
Windows XP pt-BR it will expand to "C:\Documents and Settings\YourName\Desktop"
Windows 7 en-US it will expand to "C:\Users\YourName\Desktop"
Windows 7 pt-BR it will expand to "C:\Usuarios\YourName\Desktop"
Windows XP en-US将扩展到“C:\文件和设置\”Windows XP pt-BR它将扩展到“C:\文件和设置\你的名字\桌面”Windows 7 en-US它将扩展到“C:\ \用户\你的名字\桌面”windows7 pt-BR它将扩展到“C:\通常你的名字\桌面”
On XP you can't use this to others folders exept for Desktop My documents
turning to Meus Documentos
and Local Settings
to Configuracoes locais
Personaly I thinks this is a bad thing when projecting a OS.
在XP上,你不能把它用在别人的文件夹里,我的文档转到Meus文档和本地设置,我认为这是一个不好的事情。
#5
6
Not only would that not work for an International version of Windows, it would fail if the user had edited the Registry to make their Desktop folder reside somewhere else. You can query the Registry for the file location using the REG
command:
这不仅不能用于Windows的国际版本,如果用户编辑注册表使其桌面文件夹驻留在其他地方,它也会失败。您可以使用REG命令查询文件位置的注册表:
REG QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v Desktop
To get this into a variable use something like this:
要把它变成一个变量,使用如下方法:
FOR /F "usebackq tokens=3 skip=4" %%i in (`REG QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v Desktop`) DO SET DESKTOPDIR=%%i
FOR /F "usebackq delims=" %%i in (`ECHO %DESKTOPDIR%`) DO SET DESKTOPDIR=%%i
ECHO %DESKTOPDIR%
#6
4
you could also open a DOS command prompt and execute the set command.
您还可以打开DOS命令提示符并执行set命令。
This will give you an idea what environment variables are available on your system.
这将使您了解系统中可用的环境变量。
E.g. - since you where specifically asking for a non-english Windows - heres is an example of my own German Edition (Window7-64bit) :
-因为你特别要求非英语的Windows - heres是我自己的德语版的例子(windows7 -64bit):
set > env.txt type env.txt ALLUSERSPROFILE=C:\ProgramData APPDATA=C:\Users\SOF\AppData\Roaming CommonProgramFiles=C:\Program Files\Common Files CommonProgramFiles(x86)=C:\Program Files (x86)\Common Files CommonProgramW6432=C:\Program Files\Common Files COMPUTERNAME=VMSOF ComSpec=C:\Windows\system32\cmd.exe FP_NO_HOST_CHECK=NO HOMEDRIVE=C: HOMEPATH=\Users\SOF LOCALAPPDATA=C:\Users\SOF\AppData\Local LOGONSERVER=\\VMSOF NUMBER_OF_PROCESSORS=2 OS=Windows_NT Path=C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\TortoiseSVN\bin;C:\Program Files (x86)\CMake 2.8\bin;C:\Program Files (x86)\emacs-22.3\bin;C:\Program Files (x86)\GnuWin32\bin; PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC PROCESSOR_ARCHITECTURE=AMD64 PROCESSOR_IDENTIFIER=AMD64 Family 15 Model 67 Stepping 3, AuthenticAMD PROCESSOR_LEVEL=15 PROCESSOR_REVISION=4303 ProgramData=C:\ProgramData ProgramFiles=C:\Program Files ProgramFiles(x86)=C:\Program Files (x86) ProgramW6432=C:\Program Files PROMPT=$P$G PSModulePath=C:\Windows\system32\WindowsPowerShell\v1.0\Modules\ PUBLIC=C:\Users\Public SESSIONNAME=Console SystemDrive=C: SystemRoot=C:\Windows TEMP=C:\Users\SOF\AppData\Local\Temp TMP=C:\Users\SOF\AppData\Local\Temp USERDOMAIN=VMSOF USERNAME=SOF USERPROFILE=C:\Users\SOF VBOX_INSTALL_PATH=C:\Program Files\Sun\VirtualBox\ VS90COMNTOOLS=C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\Tools\ windir=C:\Windows
#7
3
in windows 7 this returns the desktop path:
在windows 7中,这将返回桌面路径:
FOR /F "usebackq tokens=3 " %%i in (`REG QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v Desktop`) DO SET DESKTOPDIR=%%i FOR /F "usebackq delims=" %%i in (`ECHO %DESKTOPDIR%`) DO SET DESKTOPDIR=%%i ECHO %DESKTOPDIR%
#8
2
This is not a solution but I hope it helps: This comes close except that when the KEY = %userprofile%\desktop the copy fails even though zdesktop=%userprofile%\desktop. I think because the embedded %userprofile% is not getting translated.
这不是一个解决方案,但我希望它能有所帮助:这是很接近的,除了当KEY =%userprofile%\desktop时,复制失败,即使zdesktop=%userprofile%\desktop。我认为因为嵌入的%userprofile%没有被翻译。
REG QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v Desktop>z.out
for /f "tokens=3 skip=4" %%t in (z.out) do set zdesktop=%%t
copy myicon %zdesktop%
set zdesktop=
del z.out
So it sucessfully parses out the REG key but if the key contains an embedded %var% it doesn't get translated during the copy command.
因此,它成功地解析了REG键,但是如果键包含一个嵌入的%var%,它在复制命令中不会被翻译。
#9
2
I had a similar problem (and VBScript or PowerShell was not an option) and the code I found in this article did not work for me. I had problems with OS versions and language versions. After some experiments I've come to this solution:
我有一个类似的问题(VBScript或PowerShell不是一个选项),我在这篇文章中找到的代码对我不起作用。我有操作系统版本和语言版本的问题。经过一些实验,我找到了这个解决方案:
for /f "usebackq tokens=2,3*" %%A in (`REG QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v "Desktop"`) do if %%A==REG_EXPAND_SZ call :reparse set desktopdir=%%B
echo %desktopdir%
goto :EOF
:reparse
%*
goto :EOF
This code works for me in English and Polish versions of Windows 7 and Windows XP.
这段代码适用于我的英语和波兰版本的Windows 7和Windows XP。
The :reparse subroutine allows for delayed expansion of environment variables.
reparse子例程允许延迟扩展环境变量。
#10
2
While I realize this is a bit of an older post, I thought this might help people in a similar situation. I made a quick one line VBScript to pull info for whatever special folder you would like (no error checking though) and it works like this:
当我意识到这是一个较老的帖子时,我认为这可能会帮助人们在类似的情况下。我做了一个快速的一行VBScript来获取你想要的任何特殊文件夹的信息(没有错误检查),它是这样工作的:
Create a file "GetShellFolder.vbs" with the following line:
创建一个文件“GetShellFolder。vbs“有以下一行:
WScript.Echo WScript.CreateObject("WScript.Shell").SpecialFolders(WScript.Arguments(0))
I always make sure to copy cscript.exe (32-bit version) to the same folder as the batch file I am running this from, I will assume you are doing the same (I have had situations where users have somehow removed C:\Windows\system32 from their path, or managed to get rid of cscript.exe, or it's infected or otherwise doesn't work).
我总是确保拷贝cscript。exe(32位版本)相同的文件夹我运行这个批处理文件,我将假设您也在做着同样的事情(我有用户的情况下删除C:\Windows\system32从他们的路径,或设法摆脱cscript。exe,或者它被感染了,或者其他方法不起作用。
Now copy the file to be copied to the same folder and create a batch file in there with the following lines:
现在将该文件复制到同一个文件夹,并在其中创建一个批处理文件,其中包含以下几行:
for /f "delims=" %%i in ('^""%~dp0cscript.exe" "%~dp0GetShellFolder.vbs" "Desktop" //nologo^"') DO SET SHELLDIR=%%i
copy /y "%~dp0<file_to_copy>" "%SHELLDIR%\<file_to_copy>"
In the above code you can replace "Desktop" with any valid special folder (Favorites, StartMenu, etc. - the full official list is at https://msdn.microsoft.com/en-us/library/0ea7b5xe%28v=vs.84%29.aspx) and of course <file_to_copy>
with the actual file you want placed there. This saves you from trying to access the registry (which you can't do as a limited user anyway) and should be simple enough to adapt to multiple applications.
在上面的代码中,您可以使用任何有效的特殊文件夹(Favorites, StartMenu,等等)来替换“Desktop”,完整的正式列表是https://msdn.microsoft.com/en- us/library/0ea7b5xe%28vvs.84%2xe%28v. aspx),当然
Oh and for those that don't know the "%~dp0"
is just the directory from which the script is being called. It works for UNC paths as well which makes the batch file using it extremely portable. That specifically ends in a trailing "\" though so it can look a little odd at first glance.
噢,对于那些不知道“%~dp0”的人,这只是一个被调用脚本的目录。它也适用于UNC路径,这使得批处理文件的使用非常方便。不过,它的结尾是“\”,所以乍一看可能有点奇怪。
#11
1
@Dave Webb's answer is probably the way to go. The only other thing I can think of are the CSIDLs:
戴夫·韦伯(dave Webb)的回答可能是解决问题的方法。我能想到的另一件事是CSIDLs:
CSIDL_DESKTOPDIRECTORY
CSIDL_DESKTOPDIRECTORY
The file system directory used to physically store file objects on the desktop (which should not be confused with the desktop folder itself). A typical path is C:\Documents and Settings\username\Desktop.
文件系统目录用于物理存储在桌面上的文件对象(不应该与桌面文件夹本身混淆)。一个典型的路径C:\Documents and Settings\username\Desktop.
I have no idea how to get hold of those from the command line, though.
不过,我不知道如何从命令行获取这些信息。
#12
1
Multilingual Version, tested on Japanese OS
Batch File
多语言版本,在日本OS批处理文件上测试。
set getdesk=REG QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v Desktop
FOR /f "delims=(=" %%G IN ('%getdesk% ^|find "_SZ"') DO set desktop=%%G
set desktop1=%desktop:*USERPROFILE%\=%
cd "%userprofile%\%desktop1%"
set getdesk=
set desktop1=
set desktop=
#13
1
This should work no matter what language version of Windows it is and no matter where the folder is located. It also doesn't matter whether there are any spaces in the folder path.
无论Windows的语言版本是什么,无论文件夹位于何处,它都应该工作。在文件夹路径中是否有空格也无关紧要。
FOR /F "tokens=2*" %%A IN ('REG QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v Desktop^|FIND/I "desktop"') DO SET Desktop=%%B
ECHO %Desktop%
In case of Windows 2000 (and probably NT 4.0) you need to copy reg.exe to the %windir% folder manually since it is not available there by default.
如果是Windows 2000(可能是NT 4.0),您需要复制reg。由于默认情况下无法在该文件夹中使用,请手动执行%windir%文件夹。
#14
1
I use this code to get the User desktop and Public desktop paths from the registry, tested on Windows XP SP2 pt-PT and Windows 10 b14393 en-US, so it probably works in Vista/7/8 and other languages.
我使用此代码从注册表中获取用户桌面和公共桌面路径,在Windows XP SP2 pt-PT和Windows 10 b14393 en-US上进行测试,因此它可能在Vista/7/8和其他语言中运行。
:: get user desktop and public desktop paths
for /f "tokens=* delims= " %%a in ('reg query "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v Desktop ^|find /i "REG_"') do set "batch_userdesktop=%%a"
for /f "tokens=* delims= " %%a in ('reg query "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v "Common Desktop" ^|find /i "REG_"') do set "batch_publicdesktop=%%a"
:: remove everything up to and including "_SZ"
set "batch_userdesktop=%batch_userdesktop:*_sz=%"
set "batch_publicdesktop=%batch_publicdesktop:*_sz=%%
:: remove leading spaces and TABs
:loop
if "%batch_userdesktop:~0,1%"==" " set "batch_userdesktop=%batch_userdesktop:~1%" & goto loop
if "%batch_publicdesktop:~0,1%"==" " set "batch_publicdesktop=%batch_publicdesktop:~1%" & goto loop
if "%batch_userdesktop:~0,1%"==" " set "batch_userdesktop=%batch_userdesktop:~1%" & goto loop
if "%batch_publicdesktop:~0,1%"==" " set "batch_publicdesktop=%batch_publicdesktop:~1%" & goto loop
The last two lines include a TAB inside the " ", some text editors add spaces when you press TAB, so make sure you have an actual TAB instead of spaces.
最后两行包括“”中的一个选项卡,一些文本编辑器在按TAB时添加空格,所以要确保有一个实际的选项卡,而不是空格。
I'm not sure the code requires setlocal enabledelayedexpansion, it's part of my SETVARS.CMD which I call from other batches to set common variables like cpu architecture, account language, windows version and service pack, path to user/public desktop, etc.
我不确定代码是否需要setlocal enabledelayedexpansion,它是我的SETVARS的一部分。我从其他批次调用CMD来设置公共变量,如cpu架构、帐户语言、windows版本和服务包、用户/公共桌面的路径等。
#15
0
I know this is kind of an old topic, but I would use the Powershell variable
我知道这是一个老话题,但我会使用Powershell变量。
$env:UserProfile
To use it to get to the desktop, it would be:
要用它到桌面,它应该是:
cd $env:UserProfile\desktop
This works both locally and remotely on windows 7. Hope this is useful as I ran across a situation where a client machine didn't have a value in $home.
这在本地和远程都适用于windows 7。希望这是有用的,因为我遇到了这样的情况:客户机器在$home中没有价值。
#1
13
I found that the best solution is to use a vbscript together with the batch file.
我发现最好的解决方案是与批处理文件一起使用vbscript。
Here is the batch file:
这是批处理文件:
@ECHO OFF
FOR /F "usebackq delims=" %%i in (`cscript findDesktop.vbs`) DO SET DESKTOPDIR=%%i
ECHO %DESKTOPDIR%
Here is findDesktop.vbs file:
这是findDesktop。根据文件:
set WshShell = WScript.CreateObject("WScript.Shell")
strDesktop = WshShell.SpecialFolders("Desktop")
wscript.echo(strDesktop)
There may be other solutions but I personally find this one less hackish.
也许有其他的解决办法,但我个人觉得这是一个不那么陈腐的方法。
I tested this on an English PC and also a French PC - it seems to work (Windows XP).
我在一台英国个人电脑和一台法国电脑上测试了这款软件,它似乎很好用(Windows XP)。
HTH,
HTH,
Iulian Şerbănoiu
IulianŞerbănoiu
#2
49
To be safe, you should use the proper APIs in Powershell (or VBScript)
Using PowerShell:
为了安全起见,您应该使用Powershell(或VBScript)中的适当api:
[Environment]::GetFolderPath("Desktop")
Copy something using Powershell:
复制使用Powershell的东西:
Copy-Item $home\*.txt ([Environment]::GetFolderPath("Desktop"))
Here is a VBScript-example to get the desktop path:
下面是一个用于获取桌面路径的vbscript示例:
dim WSHShell, desktop, pathstring, objFSO
set objFSO=CreateObject("Scripting.FileSystemObject")
Set WSHshell = CreateObject("WScript.Shell")
desktop = WSHShell.SpecialFolders("Desktop")
pathstring = objFSO.GetAbsolutePathName(desktop)
WScript.Echo pathstring
#3
13
KB's answer to use [Environment]::GetFolderPath("Desktop")
is obviously the official Windows API for doing this.
KB使用[环境]::GetFolderPath(“桌面”)的答案显然是执行此操作的官方Windows API。
However, if you're working interactively at the prompt, or just want something that works on your machine, the tilda (~) character refers to the current user's home folder. So ~/desktop
is the user's desktop folder.
但是,如果您是在提示符之间交互工作,或者只是想要在您的机器上运行的东西,那么tilda(~)字符是指当前用户的主文件夹。所以~/桌面是用户的桌面文件夹。
#4
11
At least on Windows XP, Vista and 7 you can use the "%UserProfile%\Desktop"
safely.
至少在Windows XP、Vista和7上,你可以安全地使用“%UserProfile%\Desktop”。
Windows XP en-US it will expand to "C:\Documents and Settings\YourName\Desktop"
Windows XP pt-BR it will expand to "C:\Documents and Settings\YourName\Desktop"
Windows 7 en-US it will expand to "C:\Users\YourName\Desktop"
Windows 7 pt-BR it will expand to "C:\Usuarios\YourName\Desktop"
Windows XP en-US将扩展到“C:\文件和设置\”Windows XP pt-BR它将扩展到“C:\文件和设置\你的名字\桌面”Windows 7 en-US它将扩展到“C:\ \用户\你的名字\桌面”windows7 pt-BR它将扩展到“C:\通常你的名字\桌面”
On XP you can't use this to others folders exept for Desktop My documents
turning to Meus Documentos
and Local Settings
to Configuracoes locais
Personaly I thinks this is a bad thing when projecting a OS.
在XP上,你不能把它用在别人的文件夹里,我的文档转到Meus文档和本地设置,我认为这是一个不好的事情。
#5
6
Not only would that not work for an International version of Windows, it would fail if the user had edited the Registry to make their Desktop folder reside somewhere else. You can query the Registry for the file location using the REG
command:
这不仅不能用于Windows的国际版本,如果用户编辑注册表使其桌面文件夹驻留在其他地方,它也会失败。您可以使用REG命令查询文件位置的注册表:
REG QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v Desktop
To get this into a variable use something like this:
要把它变成一个变量,使用如下方法:
FOR /F "usebackq tokens=3 skip=4" %%i in (`REG QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v Desktop`) DO SET DESKTOPDIR=%%i
FOR /F "usebackq delims=" %%i in (`ECHO %DESKTOPDIR%`) DO SET DESKTOPDIR=%%i
ECHO %DESKTOPDIR%
#6
4
you could also open a DOS command prompt and execute the set command.
您还可以打开DOS命令提示符并执行set命令。
This will give you an idea what environment variables are available on your system.
这将使您了解系统中可用的环境变量。
E.g. - since you where specifically asking for a non-english Windows - heres is an example of my own German Edition (Window7-64bit) :
-因为你特别要求非英语的Windows - heres是我自己的德语版的例子(windows7 -64bit):
set > env.txt type env.txt ALLUSERSPROFILE=C:\ProgramData APPDATA=C:\Users\SOF\AppData\Roaming CommonProgramFiles=C:\Program Files\Common Files CommonProgramFiles(x86)=C:\Program Files (x86)\Common Files CommonProgramW6432=C:\Program Files\Common Files COMPUTERNAME=VMSOF ComSpec=C:\Windows\system32\cmd.exe FP_NO_HOST_CHECK=NO HOMEDRIVE=C: HOMEPATH=\Users\SOF LOCALAPPDATA=C:\Users\SOF\AppData\Local LOGONSERVER=\\VMSOF NUMBER_OF_PROCESSORS=2 OS=Windows_NT Path=C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\TortoiseSVN\bin;C:\Program Files (x86)\CMake 2.8\bin;C:\Program Files (x86)\emacs-22.3\bin;C:\Program Files (x86)\GnuWin32\bin; PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC PROCESSOR_ARCHITECTURE=AMD64 PROCESSOR_IDENTIFIER=AMD64 Family 15 Model 67 Stepping 3, AuthenticAMD PROCESSOR_LEVEL=15 PROCESSOR_REVISION=4303 ProgramData=C:\ProgramData ProgramFiles=C:\Program Files ProgramFiles(x86)=C:\Program Files (x86) ProgramW6432=C:\Program Files PROMPT=$P$G PSModulePath=C:\Windows\system32\WindowsPowerShell\v1.0\Modules\ PUBLIC=C:\Users\Public SESSIONNAME=Console SystemDrive=C: SystemRoot=C:\Windows TEMP=C:\Users\SOF\AppData\Local\Temp TMP=C:\Users\SOF\AppData\Local\Temp USERDOMAIN=VMSOF USERNAME=SOF USERPROFILE=C:\Users\SOF VBOX_INSTALL_PATH=C:\Program Files\Sun\VirtualBox\ VS90COMNTOOLS=C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\Tools\ windir=C:\Windows
#7
3
in windows 7 this returns the desktop path:
在windows 7中,这将返回桌面路径:
FOR /F "usebackq tokens=3 " %%i in (`REG QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v Desktop`) DO SET DESKTOPDIR=%%i FOR /F "usebackq delims=" %%i in (`ECHO %DESKTOPDIR%`) DO SET DESKTOPDIR=%%i ECHO %DESKTOPDIR%
#8
2
This is not a solution but I hope it helps: This comes close except that when the KEY = %userprofile%\desktop the copy fails even though zdesktop=%userprofile%\desktop. I think because the embedded %userprofile% is not getting translated.
这不是一个解决方案,但我希望它能有所帮助:这是很接近的,除了当KEY =%userprofile%\desktop时,复制失败,即使zdesktop=%userprofile%\desktop。我认为因为嵌入的%userprofile%没有被翻译。
REG QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v Desktop>z.out
for /f "tokens=3 skip=4" %%t in (z.out) do set zdesktop=%%t
copy myicon %zdesktop%
set zdesktop=
del z.out
So it sucessfully parses out the REG key but if the key contains an embedded %var% it doesn't get translated during the copy command.
因此,它成功地解析了REG键,但是如果键包含一个嵌入的%var%,它在复制命令中不会被翻译。
#9
2
I had a similar problem (and VBScript or PowerShell was not an option) and the code I found in this article did not work for me. I had problems with OS versions and language versions. After some experiments I've come to this solution:
我有一个类似的问题(VBScript或PowerShell不是一个选项),我在这篇文章中找到的代码对我不起作用。我有操作系统版本和语言版本的问题。经过一些实验,我找到了这个解决方案:
for /f "usebackq tokens=2,3*" %%A in (`REG QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v "Desktop"`) do if %%A==REG_EXPAND_SZ call :reparse set desktopdir=%%B
echo %desktopdir%
goto :EOF
:reparse
%*
goto :EOF
This code works for me in English and Polish versions of Windows 7 and Windows XP.
这段代码适用于我的英语和波兰版本的Windows 7和Windows XP。
The :reparse subroutine allows for delayed expansion of environment variables.
reparse子例程允许延迟扩展环境变量。
#10
2
While I realize this is a bit of an older post, I thought this might help people in a similar situation. I made a quick one line VBScript to pull info for whatever special folder you would like (no error checking though) and it works like this:
当我意识到这是一个较老的帖子时,我认为这可能会帮助人们在类似的情况下。我做了一个快速的一行VBScript来获取你想要的任何特殊文件夹的信息(没有错误检查),它是这样工作的:
Create a file "GetShellFolder.vbs" with the following line:
创建一个文件“GetShellFolder。vbs“有以下一行:
WScript.Echo WScript.CreateObject("WScript.Shell").SpecialFolders(WScript.Arguments(0))
I always make sure to copy cscript.exe (32-bit version) to the same folder as the batch file I am running this from, I will assume you are doing the same (I have had situations where users have somehow removed C:\Windows\system32 from their path, or managed to get rid of cscript.exe, or it's infected or otherwise doesn't work).
我总是确保拷贝cscript。exe(32位版本)相同的文件夹我运行这个批处理文件,我将假设您也在做着同样的事情(我有用户的情况下删除C:\Windows\system32从他们的路径,或设法摆脱cscript。exe,或者它被感染了,或者其他方法不起作用。
Now copy the file to be copied to the same folder and create a batch file in there with the following lines:
现在将该文件复制到同一个文件夹,并在其中创建一个批处理文件,其中包含以下几行:
for /f "delims=" %%i in ('^""%~dp0cscript.exe" "%~dp0GetShellFolder.vbs" "Desktop" //nologo^"') DO SET SHELLDIR=%%i
copy /y "%~dp0<file_to_copy>" "%SHELLDIR%\<file_to_copy>"
In the above code you can replace "Desktop" with any valid special folder (Favorites, StartMenu, etc. - the full official list is at https://msdn.microsoft.com/en-us/library/0ea7b5xe%28v=vs.84%29.aspx) and of course <file_to_copy>
with the actual file you want placed there. This saves you from trying to access the registry (which you can't do as a limited user anyway) and should be simple enough to adapt to multiple applications.
在上面的代码中,您可以使用任何有效的特殊文件夹(Favorites, StartMenu,等等)来替换“Desktop”,完整的正式列表是https://msdn.microsoft.com/en- us/library/0ea7b5xe%28vvs.84%2xe%28v. aspx),当然
Oh and for those that don't know the "%~dp0"
is just the directory from which the script is being called. It works for UNC paths as well which makes the batch file using it extremely portable. That specifically ends in a trailing "\" though so it can look a little odd at first glance.
噢,对于那些不知道“%~dp0”的人,这只是一个被调用脚本的目录。它也适用于UNC路径,这使得批处理文件的使用非常方便。不过,它的结尾是“\”,所以乍一看可能有点奇怪。
#11
1
@Dave Webb's answer is probably the way to go. The only other thing I can think of are the CSIDLs:
戴夫·韦伯(dave Webb)的回答可能是解决问题的方法。我能想到的另一件事是CSIDLs:
CSIDL_DESKTOPDIRECTORY
CSIDL_DESKTOPDIRECTORY
The file system directory used to physically store file objects on the desktop (which should not be confused with the desktop folder itself). A typical path is C:\Documents and Settings\username\Desktop.
文件系统目录用于物理存储在桌面上的文件对象(不应该与桌面文件夹本身混淆)。一个典型的路径C:\Documents and Settings\username\Desktop.
I have no idea how to get hold of those from the command line, though.
不过,我不知道如何从命令行获取这些信息。
#12
1
Multilingual Version, tested on Japanese OS
Batch File
多语言版本,在日本OS批处理文件上测试。
set getdesk=REG QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v Desktop
FOR /f "delims=(=" %%G IN ('%getdesk% ^|find "_SZ"') DO set desktop=%%G
set desktop1=%desktop:*USERPROFILE%\=%
cd "%userprofile%\%desktop1%"
set getdesk=
set desktop1=
set desktop=
#13
1
This should work no matter what language version of Windows it is and no matter where the folder is located. It also doesn't matter whether there are any spaces in the folder path.
无论Windows的语言版本是什么,无论文件夹位于何处,它都应该工作。在文件夹路径中是否有空格也无关紧要。
FOR /F "tokens=2*" %%A IN ('REG QUERY "HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v Desktop^|FIND/I "desktop"') DO SET Desktop=%%B
ECHO %Desktop%
In case of Windows 2000 (and probably NT 4.0) you need to copy reg.exe to the %windir% folder manually since it is not available there by default.
如果是Windows 2000(可能是NT 4.0),您需要复制reg。由于默认情况下无法在该文件夹中使用,请手动执行%windir%文件夹。
#14
1
I use this code to get the User desktop and Public desktop paths from the registry, tested on Windows XP SP2 pt-PT and Windows 10 b14393 en-US, so it probably works in Vista/7/8 and other languages.
我使用此代码从注册表中获取用户桌面和公共桌面路径,在Windows XP SP2 pt-PT和Windows 10 b14393 en-US上进行测试,因此它可能在Vista/7/8和其他语言中运行。
:: get user desktop and public desktop paths
for /f "tokens=* delims= " %%a in ('reg query "HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v Desktop ^|find /i "REG_"') do set "batch_userdesktop=%%a"
for /f "tokens=* delims= " %%a in ('reg query "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\User Shell Folders" /v "Common Desktop" ^|find /i "REG_"') do set "batch_publicdesktop=%%a"
:: remove everything up to and including "_SZ"
set "batch_userdesktop=%batch_userdesktop:*_sz=%"
set "batch_publicdesktop=%batch_publicdesktop:*_sz=%%
:: remove leading spaces and TABs
:loop
if "%batch_userdesktop:~0,1%"==" " set "batch_userdesktop=%batch_userdesktop:~1%" & goto loop
if "%batch_publicdesktop:~0,1%"==" " set "batch_publicdesktop=%batch_publicdesktop:~1%" & goto loop
if "%batch_userdesktop:~0,1%"==" " set "batch_userdesktop=%batch_userdesktop:~1%" & goto loop
if "%batch_publicdesktop:~0,1%"==" " set "batch_publicdesktop=%batch_publicdesktop:~1%" & goto loop
The last two lines include a TAB inside the " ", some text editors add spaces when you press TAB, so make sure you have an actual TAB instead of spaces.
最后两行包括“”中的一个选项卡,一些文本编辑器在按TAB时添加空格,所以要确保有一个实际的选项卡,而不是空格。
I'm not sure the code requires setlocal enabledelayedexpansion, it's part of my SETVARS.CMD which I call from other batches to set common variables like cpu architecture, account language, windows version and service pack, path to user/public desktop, etc.
我不确定代码是否需要setlocal enabledelayedexpansion,它是我的SETVARS的一部分。我从其他批次调用CMD来设置公共变量,如cpu架构、帐户语言、windows版本和服务包、用户/公共桌面的路径等。
#15
0
I know this is kind of an old topic, but I would use the Powershell variable
我知道这是一个老话题,但我会使用Powershell变量。
$env:UserProfile
To use it to get to the desktop, it would be:
要用它到桌面,它应该是:
cd $env:UserProfile\desktop
This works both locally and remotely on windows 7. Hope this is useful as I ran across a situation where a client machine didn't have a value in $home.
这在本地和远程都适用于windows 7。希望这是有用的,因为我遇到了这样的情况:客户机器在$home中没有价值。