Monday, March 16, 2009

Information on %~dp0

This macro is used to get the path of batch file.

It doesn't matter from where you are calling batch file it will return the path where the batch file is stored.

So if you put the following in a batch file, no matter what directory you are in when you run it, it will print the directory the batch file is in, and do a directory listing.

e.g. :
echo Batch file path is %~dp0
dir %~dp0

Tips on Creating Batch Files

The PATH variable can be set in two ways:

SET PATH=c:\bat;c:\dos

or:

PATH c:\bat;c:\dos

In the first case, using SET, the PATH variable is set to c:\bat;c:\dos, in lower case, as specified.
In the second case, using PATH, the PATH variable is set to C:\BAT;C:\DOS, in upper case, no matter how it was specified.


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 e.g. 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 D:\dirshow.bat:

Code:
@echo off
echo this is %%cd%% %cd%
echo this is %%~dp0 %~dp0

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

Code:
C:\>D:\dirshow.bat
this is %cd% C:\
this is %~dp0 D:\

Sunday, March 15, 2009

Contact Us

Please  leave your valuable comments here... If you have any suggestions or if you want me to put a article on any topic of your choice please leave a comment here... alternatively you can send me a mail at mailme.blogadda[at]gmail.com

Enjoy the Programming..