The difference between "% ~ dp0" and ". \"?

Let's say I use a batch file and want it to be directed to a folder located in the same package directory. If I am not mistaken, you should write "% ~ dp0 \ whateverfoldername". But you cannot do the same just by writing ". \ Whateverfoldername"? If so, what is the difference and / or advantage of the respective team?

+7
source share
2 answers
pushd %~dp0 

often used to change the source directory from which the batch was run. This is very useful in new OSes, when the user can "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 an “Administrator” in Vista, Win 7, Win 8, 2008 Server or 2012 Server. See what happens?

+17
source

".\ will be relative to the CURRENT directory, so if you change directories using the CD command, then you will look for the THAT directory, not the directory where the package is located.

In fact, it’s normal to create a separate directory, often called \batch or perhaps \belfry to save batch files. If ,bat is at the path mark, it will be located. %dp0 will result in a .bat location.

+3
source

All Articles