The difference between %CD% and %~dp0

It seems both can get the current directory. What is the difference between %~dp0 and %CD%, then?

  • First, %~dp0 can only be used in bat file while %CD% can be used on command line.
  • Second, for %CD%, the current directory means the directory when executing the command line or the batch file. For %~dp0, the current directory is the directory where the bat file resides. So if you put the batch file in c:\dir\test.bat and the file contains @echo %CD%, if you are now in C:\dir and execute test.bat, it will output c:\dir; if you are in c:\ and execute c:\dir\test.bat, it will output c:\. If the bat file has the content @echo %~dp0, whether you are in c:\ and execute c:\dir\test.bat, or in c:\dir and execute test.bat, the output is the same: c:\dir\. Note there is a back slash at the end.
Did you like this?
Tip admin with Cryptocurrency

Donate Bitcoin to admin

Scan to Donate Bitcoin to admin
Scan the QR code or copy the address below into your wallet to send some bitcoin:

Donate Bitcoin Cash to admin

Scan to Donate Bitcoin Cash to admin
Scan the QR code or copy the address below into your wallet to send bitcoin:

Donate Ethereum to admin

Scan to Donate Ethereum to admin
Scan the QR code or copy the address below into your wallet to send some Ether:

Donate Litecoin to admin

Scan to Donate Litecoin to admin
Scan the QR code or copy the address below into your wallet to send some Litecoin:

Donate Monero to admin

Scan to Donate Monero to admin
Scan the QR code or copy the address below into your wallet to send some Monero:

Donate ZCash to admin

Scan to Donate ZCash to admin
Scan the QR code or copy the address below into your wallet to send some ZCash:
Posted in

Leave a Reply