In DOS batches, it is sometimes necessary to know the current path. In DOS, there are two environment variables that can relate to the current path, one is%cd%, and the other is%~dp0.
The use of these two variables and the content of the representative is generally different.
1.%cd% can be used in a batch file or in a command line, and after expansion, the drive letter: + current directory, such as:
Under the C:/dir,
BAT code echo%cd%
Echo%cd%
Shown as,
BAT Code C:/dir
C:/dir
%cd% 's time content can be changed, such as the CD command can change its content.
2. The%~dp0 can only be used in a batch file, which is determined by the directory location of the batch file in which it resides, and is the disk character of the batch file: + path. In the process of executing this batch file, the content that it expands can not be changed.
For example, under D disk, there is a batch file, Dirshow.bat:
BAT code @echo off the echo this is%%cd%%%cd% the echo this is%%~dp0%~dp0
@echo off the echo this is
%%cd%%%cd% The echo this is
%%~dp0%~dp0
In c:/ To execute it, the output is:
BAT code c:/>d:/dirshow.bat This are%cd% c:/this is%~dp0 d:/