% ~ dp0은 무엇을 의미하며 어떻게 작동합니까?
나는 %~dp0매우 유용하다고 생각하고 배치 파일을 더 이식하기 위해 많이 사용합니다.
하지만 라벨 자체는 나에게 매우 수수께끼처럼 보입니다 ... 무슨 ~일입니까? dp드라이브와 경로를 의미 합니까 ? 에서 파일 이름이 포함 된 배치 파일의 경로를 0참조 합니까 %0?
아니면 그냥 이상한 라벨일까요?
또한 문서화 된 기능인지 아니면 더 이상 사용되지 않는 기능인지 알고 싶습니다.
부름
for /?
명령 줄에서이 구문에 대한 도움말을 제공합니다 (FOR 외부에서도 사용할 수 있으며 도움말을 찾을 수있는 곳입니다).
또한 FOR 변수 참조의 대체가 향상되었습니다. 이제 다음과 같은 선택적 구문을 사용할 수 있습니다.
%~I - expands %I removing any surrounding quotes (") %~fI - expands %I to a fully qualified path name %~dI - expands %I to a drive letter only %~pI - expands %I to a path only %~nI - expands %I to a file name only %~xI - expands %I to a file extension only %~sI - expanded path contains short names only %~aI - expands %I to file attributes of file %~tI - expands %I to date/time of file %~zI - expands %I to size of file %~$PATH:I - searches the directories listed in the PATH environment variable and expands %I to the fully qualified name of the first one found. If the environment variable name is not defined or the file is not found by the search, then this modifier expands to the empty string수정자를 결합하여 복합 결과를 얻을 수 있습니다.
%~dpI - expands %I to a drive letter and path only %~nxI - expands %I to a file name and extension only %~fsI - expands %I to a full path name with short names only %~dp$PATH:I - searches the directories listed in the PATH environment variable for %I and expands to the drive letter and path of the first one found. %~ftzaI - expands %I to a DIR like output line위의 예에서 % I 및 PATH는 다른 유효한 값으로 대체 될 수 있습니다. % ~ 구문은 유효한 FOR 변수 이름으로 종료됩니다. % I와 같은 대문자 변수 이름을 선택하면 더 읽기 쉽고 대소 문자를 구분하지 않는 수정 자와의 혼동을 피할 수 있습니다.
f"전체 경로 이름", d드라이브 문자, 경로 와 같이 사용할 수있는 다른 문자 p가 있으며 조합 할 수 있습니다. %~각 시퀀스의 시작이고 숫자 I는 매개 변수에서 작동 함을 나타냅니다 %I(여기서는 %0가정 한대로 배치 파일의 전체 이름 임).
(먼저,이 유용한 참조 사이트를 일괄 적으로 추천하고 싶습니다 : http://ss64.com/nt/ )
또 다른 유용한 설명 : http://htipe.wordpress.com/2008/10/09/the-dp0-variable/
% ~ dp0 변수
Windows 배치 파일 내에서 참조 될 때 % ~ dp0 (즉, 0) 변수 는 해당 배치 파일 의 d rive 문자 및 p ath로 확장 됩니다.
% 0- % 9 변수는 배치 파일의 명령 줄 매개 변수를 참조합니다. % 1- % 9는 배치 파일 이름 뒤의 명령 줄 인수를 참조합니다. % 0은 배치 파일 자체를 나타냅니다.
퍼센트 문자 (%) 뒤에 물결표 문자 (~)가있는 경우 매개 변수 번호 앞에 수정자를 삽입하여 변수가 확장되는 방식을 변경할 수 있습니다. d 수정자는 드라이브 문자로 확장되고 p 수정자는 매개 변수의 경로로 확장됩니다.
예 : C :에 bat_files라는 디렉토리가 있고 그 디렉토리에 example.bat라는 파일이 있다고 가정 해 보겠습니다. 이 경우 % ~ dp0 (d 및 p 수정 자 결합)은 C : \ bat_files로 확장됩니다.
자세한 설명은 이 Microsoft 문서 를 확인 하세요.
또한 이 포럼 스레드를 확인하십시오 .
그리고 여기 에서 더 명확한 참조 :
%CmdCmdLine%CMD.EXE에 전달 된 전체 명령 줄을 반환합니다.%*첫 번째 명령 줄 인수에서 시작하는 나머지 명령 줄을 반환합니다 (Windows NT 4에서는 % *에도 모든 선행 공백이 포함됨).%~dn% n이 유효한 경로 또는 파일 이름 (UNC 없음)이면 드라이브 문자 % n (n의 범위는 0-9)을 반환합니다.%~pn% n이 유효한 경로 또는 파일 이름 (UNC 없음)이면 % n의 디렉토리를 반환합니다.%~nnwill return the file name only of %n if %n is a valid file name%~xnwill return the file extension only of %n if %n is a valid file name%~fnwill return the fully qualified path of %n if %n is a valid file name or directory
ADD 1
Just found some good reference for the mysterious ~ tilde operator.
The %~ string is called percent tilde operator. You can find it in situations like: %~0.
The :~ string is called colon tilde operator. You can find it like %SOME_VAR:~0,-1%.
ADD 2 - 1:12 PM 7/6/2018
%1-%9 refer to the command line args. If they are not valid path values, %~dp1 - %~dp9 will all expand to the same value as %~dp0. But if they are valid path values, they will expand to their own driver/path value.
For example: (batch.bat)
@echo off
@echo ~dp0= %~dp0
@echo ~dp1= %~dp1
@echo ~dp2= %~dp2
@echo on
Run 1:
D:\Workbench>batch arg1 arg2
~dp0= D:\Workbench\
~dp1= D:\Workbench\
~dp2= D:\Workbench\
Run 2:
D:\Workbench>batch c:\123\a.exe e:\abc\b.exe
~dp0= D:\Workbench\
~dp1= c:\123\
~dp2= e:\abc\
http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/percent.mspx?mfr=true
The variable %0 in a batch script is set to the name of the executing batch file. The ~dp special syntax between the % and the 0 basically says to expand the variable %0 to show the drive letter and path, which gives you the current directory containing the batch file!
%~dp0 expands to current directory path of the running batch file.
To get clear understanding, let's create a batch file in a directory.
C:\script\test.bat
with contents:
@echo off
echo %~dp0
When you run it from command prompt, you will see this result:
C:\script\
Another tip that would help a lot is that to set the current directory to a different drive one would have to use %~d0 first, then cd %~dp0. This will change the directory to the batch file's drive, then change to its folder.
Alternatively, for #oneLinerLovers, as @Omni pointed out in the comments cd /d %~dp0 will change both the drive and directory :)
Hope this helps someone.
Great example from Strawberry Perl's portable shell launcher:
set drive=%~dp0
set drivep=%drive%
if #%drive:~-1%# == #\# set drivep=%drive:~0,-1%
set PATH=%drivep%\perl\site\bin;%drivep%\perl\bin;%drivep%\c\bin;%PATH%
not sure what the negative 1's doing there myself, but it works a treat!
An example would be nice - here's a trivial one
for %I in (*.*) do @echo %~xI
it lists only the EXTENSIONS of each file in current folder
for more useful variable combinations (also listed in previous response) from the CMD prompt execute: HELP FOR which contains this snippet
The modifiers can be combined to get compound results:
%~dpI - expands %I to a drive letter and path only
%~nxI - expands %I to a file name and extension only
%~fsI - expands %I to a full path name with short names only
%~dp$PATH:I - searches the directories listed in the PATH
environment variable for %I and expands to the
drive letter and path of the first one found.
%~ftzaI - expands %I to a DIR like output line
참고URL : https://stackoverflow.com/questions/5034076/what-does-dp0-mean-and-how-does-it-work
'Programing' 카테고리의 다른 글
| “POSIX”의 의미는 무엇입니까? (0) | 2020.09.29 |
|---|---|
| Python 스크립트 종료 (0) | 2020.09.28 |
| What does in XML mean? (0) | 2020.09.28 |
| Java에서 JSON을 구문 분석하는 방법 (0) | 2020.09.28 |
| Rails 정품 토큰 이해 (0) | 2020.09.28 |