user2259606
user2259606

Reputation: 73

Difference between "%~dp0" and ".\"?

Let's say I'm using a batch file and want it to direct to a folder located in the same directory of the batch. If I'm not wrong you would write "%~dp0\whateverfoldername". But can't the same done by just writing ".\whateverfoldername"? If so, what is the difference and/or advantage of the respective command?

Upvotes: 7

Views: 38166

Answers (2)

RGuggisberg
RGuggisberg

Reputation: 4750

pushd %~dp0

is often used to change to the original directory from which the batch was started. This is very useful in newer OS's when the user may 'Run as administrator' which changes the current directory for you! Try it sometime. Just make a simple bat

@echo off
echo.CD=%CD%
pushd %~dp0
echo.CD=%CD%
pause

Now run it. Now run it again 'As Administrator' on Vista, Win 7, Win 8, 2008 Server, or 2012 Server. See what happens?

Upvotes: 19

Magoo
Magoo

Reputation: 80113

".\ will locate with respect to the CURRENT directory, hence if you have changed directories with a CD command then you will be looking at THAT directory, not the directory in which the batch resides.

In fact, it's normal to create a separate directory, often called \batch or perhaps \belfry to keep batch files. Provided the ,bat in question is locared on the path, it will be located. %dp0 will yield the location of the .bat.

Upvotes: 3

Related Questions