echo %cd%
pause

윈도우즈 배치파일에서 '%cd%' 를 사용하면 현재 디렉토리를 알 수 있다. 이를 이용해 스크립트 실행경로에 이용할 수 있다.

C:\Windows\System32>echo C:\Windows\System32
C:\Windows\System32

C:\Windows\System32>pause

하지만 관리자 권한으로 실행하면 C:\Windows\System32 실행된다.

D:\test\work\test>call cd\test.bat

D:\test\work\test>echo D:\test\work\test
D:\test\work\test

D:\test\work\test>pause

그리고 다른 경로에서 실행할 경우 %cd% 는 그 실행할 디렉토리를 가리키게 된다.

echo %~dp0
pause
D:\test\work\test>call cd\test.bat

D:\test\work\test>echo D:\test\work\test\cd\
D:\test\work\test\cd\

D:\test\work\test>pause
계속하려면 아무 키나 누르십시오 . . .

스크립트 파일의 경로를 알고 싶으면 '%~dp0' 를 사용한다. 경로가 \ 로 끝나는 점만 주의하자.

https://stackoverflow.com/questions/3827567/how-to-get-the-path-of-the-batch-script-in-windows

 

How to get the path of the batch script in Windows?

I know that %0 contains the full path of the batch script, e.g. c:\path\to\my\file\abc.bat I would path to be equal to c:\path\to\my\file How could I achieve that ?

stackoverflow.com

 

728x90

+ Recent posts