데스크탑 경로에 대한 환경 변수는 무엇입니까?
Windows 배치 파일을 작성 중이고 데스크탑에 무언가를 복사하고 싶습니다. 나는 이것을 사용할 수 있다고 생각합니다.
% UserProfile % \ Desktop \
그러나 나는 아마도 영어 OS에서만 작동 할 것이라고 생각합니다. 국제화 된 버전에서 작동하는 배치 파일에서이 작업을 수행 할 수있는 방법이 있습니까?
최신 정보
다음 배치 파일을 시도했습니다.
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%
그리고이 출력을 얻었습니다.
S : \> REG QUERY "HKCU \ Software \ Microsoft \ Windows \ CurrentVersion \ Explorer \ User Shell Folders"/ v Desktop HKEY_CURRENT_USER \ Software \ Microsoft \ Windows \ CurrentVersion \ Explorer \ User 셸 폴더 데스크톱 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`) DESKTOPDIR = % i 설정 S : \> FOR / F "usebackq delims ="% i in (`ECHO ECHO is on.`) DO SET DESKTOPDIR = % i S : \> SET DESKTOPDIR = ECHO가 켜져 있습니다. S : \> ECHO ECHO가 켜져 있습니다. 에코가 켜져 있습니다.
가장 좋은 해결책은 배치 파일과 함께 vbscript를 사용하는 것입니다.
다음은 배치 파일입니다.
@ECHO OFF
FOR /F "usebackq delims=" %%i in (`cscript findDesktop.vbs`) DO SET DESKTOPDIR=%%i
ECHO %DESKTOPDIR%
다음은 findDesktop.vbs 파일입니다.
set WshShell = WScript.CreateObject("WScript.Shell")
strDesktop = WshShell.SpecialFolders("Desktop")
wscript.echo(strDesktop)
다른 솔루션이있을 수 있지만 개인적으로이 솔루션이 덜 해커라고 생각합니다.
나는 이것을 영어 PC와 프랑스어 PC에서 테스트했습니다. 작동하는 것 같습니다 (Windows XP).
HTH,
Iulian Şerbănoiu
안전을 위해
PowerShell을 사용하여 Powershell (또는 VBScript)에서 적절한 API를 사용해야 합니다.
[Environment]::GetFolderPath("Desktop")
Powershell을 사용하여 복사 :
Copy-Item $home\*.txt ([Environment]::GetFolderPath("Desktop"))
다음은 데스크탑 경로를 가져 오는 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
최소한 Windows XP, Vista 및 7에서는 "%UserProfile%\Desktop"
안전하게 사용할 수 있습니다 .
Windows XP en-US "C:\Documents and Settings\YourName\Desktop"
Windows XP pt-BR로 확장됩니다. "C:\Documents and Settings\YourName\Desktop"
Windows 7 en-US로 확장됩니다. "C:\Users\YourName\Desktop"
Windows 7 pt-BR로 확장됩니다."C:\Usuarios\YourName\Desktop"
XP에서 당신은 데스크톱에 대한 다른 폴더 exept이 사용할 수 없습니다 My documents
에 의존 Meus Documentos
하고 Local Settings
에 Configuracoes locais
Personaly 나는 OS를 투사 할 때이 나쁜 것입니다 생각합니다.
사용에 대한 KB의 대답 [Environment]::GetFolderPath("Desktop")
은 분명히이를위한 공식 Windows API입니다.
그러나 프롬프트에서 대화식으로 작업하거나 컴퓨터에서 작동하는 것을 원하는 경우 tilda (~) 문자는 현재 사용자의 홈 폴더를 나타냅니다. ~/desktop
사용자의 데스크탑 폴더도 마찬가지 입니다.
Windows의 국제 버전에서는 작동하지 않을뿐만 아니라 사용자가 레지스트리를 편집하여 데스크톱 폴더를 다른 위치에두면 실패합니다. 다음 REG
명령을 사용하여 레지스트리에서 파일 위치를 쿼리 할 수 있습니다 .
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%
DOS 명령 프롬프트를 열고 set 명령을 실행할 수도 있습니다 .
그러면 시스템에서 사용할 수있는 환경 변수가 무엇인지 알 수 있습니다.
예 : 영어가 아닌 Windows를 구체적으로 요청한 경우 여기에 내 독일어 버전 (Window7-64bit)의 예가 있습니다 .
설정> env.txt env.txt 입력 ALLUSERSPROFILE = C : \ ProgramData APPDATA = C : \ 사용자 \ SOF \ AppData \ 로밍 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 = \ 사용자 \ SOF LOCALAPPDATA = C : \ Users \ SOF \ AppData \ Local LOGONSERVER = \\ VMSOF NUMBER_OF_PROCESSORS = 2 OS = Windows_NT 경로 = C : \ Windows \ system32; C : \ Windows; C : \ Windows \ System32 \ Wbem; C : \ Windows \ System32 \ WindowsPowerShell \ v1.0 \; C : \ Program Files \ TortoiseSVN \ bin; C : \ 프로그램 파일 (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 제품군 15 모델 67 스테핑 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 프롬프트 = $ P $ G PSModulePath = C : \ Windows \ system32 \ WindowsPowerShell \ v1.0 \ Modules \ PUBLIC = C : \ Users \ Public SESSIONNAME = 콘솔 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 : \ 사용자 \ SOF VBOX_INSTALL_PATH = C : \ Program Files \ Sun \ VirtualBox \ VS90COMNTOOLS = C : \ Program Files (x86) \ Microsoft Visual Studio 9.0 \ Common7 \ Tools \ windir = C : \ Windows
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 에코 % DESKTOPDIR %
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.
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.
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:
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.
The :reparse subroutine allows for delayed expansion of environment variables.
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:
Create a file "GetShellFolder.vbs" with the following line:
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).
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.
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.
@Dave Webb's answer is probably the way to go. The only other thing I can think of are the CSIDLs:
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.
I have no idea how to get hold of those from the command line, though.
Multilingual Version, tested on Japanese OS
Batch File
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=
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.
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.
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.
:: 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.
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.
I know this is kind of an old topic, but I would use the Powershell variable
$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.
'program story' 카테고리의 다른 글
야생에서 좋은 오이 예? (0) | 2020.12.05 |
---|---|
Java Generics WildCard : (0) | 2020.12.05 |
메소드의 속성 값 읽기 (0) | 2020.12.04 |
JavaScript 비동기 콜백에서 발생한 예외를 포착 할 수 있습니까? (0) | 2020.12.04 |
WPF에서 제출 버튼을 만드는 방법은 무엇입니까? (0) | 2020.12.04 |