Monday, February 8, 2010

What is difference between %cd% and %~dp0

%cd% is available either to a batch file or at the command prompt and expands to the drive letter and path of the current directory (which can change by using the CD command)

%~dp0 is only available within a batch file and expands to the drive letter and path in which that batch file is located (which cannot change). It is obtained from %0 which is the batch file's name.

An experiment like the following shows the difference

Here is E:\dirpath.bat:

echo this is %%cd%%  %cd%
echo this is %%~dp0 %~dp0

Run it from C:\ and this is what you see

Code:
C:\>E:\dirpath.bat
this is %cd%  C:\
this is %~dp0 E:\

No comments:

Post a Comment