programing tip

Windows 배치 파일이 자체 파일 이름을 결정할 수 있습니까?

itbloger 2020. 8. 25. 07:45
반응형

Windows 배치 파일이 자체 파일 이름을 결정할 수 있습니까?


Windows 배치 파일이 자체 파일 이름을 결정할 수 있습니까?

예를 들어 배치 파일 C : \ Temp \ myScript.bat를 실행하는 경우 myScript.bat 내에 "myScript.bat"문자열을 확인할 수있는 명령이 있습니까?


예.

특수 %0변수를 사용하여 현재 파일의 경로를 가져옵니다.

%~n0확장자없이 파일 이름 만 얻으려면 작성 하십시오.

%~n0%~x0파일 이름과 확장자를 얻으려면 작성 하십시오.

%~nx0파일 이름과 확장자를 얻기 위해 쓸 수도 있습니다.


파일 이름을 얻을 수 있지만 '% ~'와 '0'사이에 배치하는 내용에 따라 전체 경로를 얻을 수도 있습니다. 선택하세요

d -- drive
p -- path
n -- file name
x -- extension
f -- full path

예를 들어, c : \ tmp \ foo.bat 내부에서 %~nx0"foo.bat"를 %~dpnx0제공하고 "c : \ tmp \ foo.bat"를 제공합니다. 당신이 귀여운 얻을 그렇다면, 조각은 항상 정규 순서로 조립 참고하려고 %~xnpd0, 당신은 여전히 ": \ TMP \ foo.bat c"를 얻을


SLaks 답변을 기반으로 다음 스크립트를 사용하여 정답이 다음과 같음을 확인했습니다.

echo The name of this file is: %~n0%~x0
echo The name of this file is: %~nx0

그리고 여기 내 테스트 스크립트가 있습니다.

@echo off
echo %0
echo %~0
echo %n0
echo %x0
echo %~n0
echo %dp0
echo %~dp0
pause

흥미로운 점은 '~'문자가 일반적으로 변수에서 따옴표를 제거 / 자르는 데 사용된다는 점을 감안할 때 % nx0 이 작동하지 않는다는 것입니다.


0배치 파일 내 매개 변수 번호의 특수한 경우 라는 점을 명심하십시오 . 여기서이 파일 은 명령 줄에 지정된대로0 의미 합니다 .

따라서 파일이 myfile.bat 인 경우 다음과 같이 여러 가지 방법으로 호출 할 수 있습니다. 각 방법 또는 사용과 다른 출력을 제공합니다 .%0%~0

myfile

myfile.bat

mydir\myfile.bat

c:\mydir\myfile.bat

"c:\mydir\myfile.bat"

위의 모든 것은 올바른 상대 위치에서 그것이 존재하는 디렉토리로 호출하는 경우 합법적 인 호출입니다. %~0마지막 예에서 따옴표를 제거하지만 %0그렇지 않습니다.

이것들은 모두 다른 결과를 제공 %0하고 %~0실제로 사용하고 싶은 것이 거의 없기 때문입니다.

설명 할 배치 파일은 다음과 같습니다.

@echo Full path and filename: %~f0
@echo Drive: %~d0
@echo Path: %~p0
@echo Drive and path: %~dp0
@echo Filename without extension: %~n0
@echo Filename with    extension: %~nx0
@echo Extension: %~x0
@echo Filename as given on command line: %0
@echo Filename as given on command line minus quotes: %~0
@REM Build from parts
@SETLOCAL
@SET drv=%~d0
@SET pth=%~p0
@SET fpath=%~dp0
@SET fname=%~n0
@SET ext=%~x0
@echo Simply Constructed name: %fpath%%fname%%ext%
@echo Fully  Constructed name: %drv%%pth%%fname%%ext%
@ENDLOCAL
pause

마법의 변수가 어떻게 작동하는지 느끼기 위해 아래 예제를 실행 해보십시오.

@echo off

SETLOCAL EnableDelayedExpansion

echo Full path and filename: %~f0
echo Drive: %~d0
echo Path: %~p0
echo Drive and path: %~dp0
echo Filename without extension: %~n0
echo Filename with    extension: %~nx0
echo Extension: %~x0

echo date time : %~t0
echo file size: %~z0

ENDLOCAL

관련 규칙은 다음과 같습니다.

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

아래는 내 초기 코드입니다.

@echo off
Set z=%%
echo.
echo %z%0.......%0
echo %z%~0......%~0
echo %z%n0......%n0
echo %z%x0......%x0
echo %z%~n0.....%~n0
echo %z%dp0.....%dp0
echo %z%~dp0....%~dp0
echo.

I noticed that file name given by %~0 and %0 is the way it was entered in the command-shell and not how that file is actually named. So if you want the literal case used for the file name you should use %~n0. However, this will leave out the file extension. But if you know the file name you could add the following code.

set b=%~0
echo %~n0%b:~8,4%

I have learned that ":~8,4%" means start at the 9th character of the variable and then show show the next 4 characters. The range is 0 to the end of the variable string. So %Path% would be very long!

fIlEnAmE.bat
012345678901

However, this is not as sound as Jool's solution (%~x0) above.

My Evidence:

C:\bin>filename.bat

%0.......filename.bat
%~0......filename.bat
. . .

C:\bin>fIlEnAmE.bat

%0.......fIlEnAmE.bat
%~0......fIlEnAmE.bat
%n0......n0
%x0......x0
%~n0.....FileName
%dp0.....dp0
%~dp0....C:\bin\

%~n0%b:~8,4%...FileName.bat

Press any key to continue . . .

C:\bin>dir
 Volume in drive C has no label.
 Volume Serial Number is CE18-5BD0

 Directory of C:\bin
. . .
05/02/2018  11:22 PM               208 FileName.bat

Here is the final code

@echo off
Set z=%%
set b=%~0
echo.
echo %z%0.......%0
echo %z%~0......%~0
echo %z%n0......%n0
echo %z%x0......%x0
echo %z%~n0.....%~n0
echo %z%dp0.....%dp0
echo %z%~dp0....%~dp0
echo.
echo A complex solution:
echo ===================================
echo %z%~n0%z%b:~8,4%z%...%~n0%b:~8,4%
echo ===================================
echo.
echo The preferred solution:
echo ===================================
echo %z%~n0%z%~x0.......%~n0%~x0
echo ===================================
pause

참고URL : https://stackoverflow.com/questions/8797983/can-a-windows-batch-file-determine-its-own-file-name

반응형