导航到桌面 [英] Navigating to Desktop
问题描述
我无法让bat文件正常工作.我在计算机上运行两个硬盘驱动器,而台式机在E:驱动器上.因此,%Userprofile%\ Desktop将我导航到E:上的桌面,但是当放入bat文件时,它试图在我的C:驱动器上创建另一个名为Desktop的文件夹.完全相同的拼写.但是,我将在另一台计算机上使用此计算机,该计算机可能会或可能不会在其C:驱动器上放置其桌面. 我正在将文件复制到另一个文件夹
I'm having trouble getting my bat file to work correctly. I run two hard drives on my computer and my desktop is on my E: drive. So %Userprofile%\Desktop navigates me to my desktop on E: , but when put into my bat file It tries to create another folder on my C: Drive called Desktop. Exactly the same spelling. But I will be using this on another computer That may or may not have their desktop on their C: drive. I am copying a file to another folder
xcopy /s "%~dp0\Folder\Folder\Folder\Folder\File.exe" "%USERPROFILE%\Desktop\WorkFiles" /Y
这将尝试创建一个名为Desktop的文件夹并将exe放入其中. 蝙蝠通过USB运行,因此%〜dp0"
This will attempt to create a Folder called Desktop and drop the exe into that. The bat is ran on a USB hence the "%~dp0"
推荐答案
您的xcopy命令在%~dp0
中包含一个尾部反斜杠,所以其中的一个反斜杠太多了.替换为:
Your xcopy command has a trailing backslash included in %~dp0
so there is one too much. Replace with:
xcopy /s /Y "%~dp0Folder\Folder\Folder\Folder\File.exe" "%USERPROFILE%\Desktop\WorkFiles\"
如果从正常位置重新放置桌面,则必须在注册表(或PowerShell/vbscript用户外壳程序文件夹)中查找实际位置.
如果未重新放置桌面,则该批次将获得正确的位置:
If you relocated your Desktop from the normal position you have to lookup the actual location in the registry (or PowerShell/vbscript user shell folders)
If the Desktop is not relocated, this batch will nevertheless get the proper location:
Set "Key=HKCU\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders"
Set "Val=Desktop"
For /F "Tokens=2*" %%A in (
'Reg Query "%Key%" /v "%Val%" 2^>Nul ^| Find "%Val%" '
) Do Set "Desktop=%%B"
Echo 'Desktop' location: %Desktop%
在不同的Windows系统上评估桌面的更好方法是使用eryksuns版本:
The better way to evaluate the Desktop on varying windows systems is to use eryksuns version:
@Echo off
for /f "usebackq delims=" %%a in (
`powershell -c "[environment]::GetFolderPath('Desktop')"`
) do set "desktop=%%a"
这篇关于导航到桌面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!