{"id":2560,"date":"2017-12-09T05:19:42","date_gmt":"2017-12-09T05:19:42","guid":{"rendered":"http:\/\/www.scmgalaxy.com\/tutorials\/?p=2560"},"modified":"2026-11-21T19:01:51","modified_gmt":"2026-11-21T19:01:51","slug":"dp0-vs-%cd","status":"publish","type":"post","link":"https:\/\/www.devopsschool.com\/blog\/dp0-vs-%cd\/","title":{"rendered":"%~dp0 VS %cd%"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><code>%~dp0<\/code> is the folder that contains the running batch file, while <code>%cd%<\/code> is the current working directory of the command prompt. They are the same only when you start the script from its own folder. If your script needs files stored next to it, use <code>%~dp0<\/code>.<\/p>\n\n\n<strong>rajeshkumar created the topic: %~dp0 VS %cd%<\/strong>\n\nUse of %~dp0 and Use of %cd%\n\nThey are not equivalent.\n\n%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)\n\n%~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&#8217;s name.\n\nAn experiment like the following shows the difference\n\nHere is D:\\dirshow.bat:\n\n<code>@echo off\necho this is %%cd%%  %cd%\necho this is %%~dp0 %~dp0<\/code>\n\nRun it from C:\\ and this is what you see\n\n<code>C:\\&gt;D:\\dirshow.bat\nthis is %cd%  C:\\\nthis is %~dp0 D:\\<\/code>\n\nRegards,\nRajesh Kumar\nTwitt me @ <a href=\"http:\/\/twitter.com\/RajeshKumarIn\" rel=\"noopener\">twitter.com\/RajeshKumarIn<\/a>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Quick comparison<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th><\/th><th>%cd%<\/th><th>%~dp0<\/th><\/tr><\/thead><tbody><tr><td>Returns<\/td><td>Current working directory<\/td><td>Drive and folder of the batch file<\/td><\/tr><tr><td>Works in<\/td><td>Batch files and the interactive prompt<\/td><td>Batch files only (printed literally at the prompt)<\/td><\/tr><tr><td>Changes when<\/td><td>You run <code>cd<\/code>, <code>pushd<\/code> or start the script from elsewhere<\/td><td>Never changes during the script<\/td><\/tr><tr><td>Trailing backslash<\/td><td>Only at a drive root, such as <code>C:\\<\/code><\/td><td>Always, such as <code>D:\\scripts\\<\/code><\/td><\/tr><tr><td>Use it for<\/td><td>Paths relative to where the user is working<\/td><td>Files shipped next to the script<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">How %~dp0 is built<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><code>%0<\/code> is the name used to start the batch file. The <code>~<\/code> modifiers extract parts of it, and you can combine them:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>Modifier<\/th><th>Result for D:\\scripts\\dirshow.bat<\/th><\/tr><\/thead><tbody><tr><td><code>%~0<\/code><\/td><td>The name as typed, with surrounding quotes removed<\/td><\/tr><tr><td><code>%~f0<\/code><\/td><td><code>D:\\scripts\\dirshow.bat<\/code> (full path)<\/td><\/tr><tr><td><code>%~d0<\/code><\/td><td><code>D:<\/code><\/td><\/tr><tr><td><code>%~p0<\/code><\/td><td><code>\\scripts\\<\/code><\/td><\/tr><tr><td><code>%~dp0<\/code><\/td><td><code>D:\\scripts\\<\/code><\/td><\/tr><tr><td><code>%~n0<\/code><\/td><td><code>dirshow<\/code><\/td><\/tr><tr><td><code>%~x0<\/code><\/td><td><code>.bat<\/code><\/td><\/tr><tr><td><code>%~nx0<\/code><\/td><td><code>dirshow.bat<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">The same modifiers work on arguments (<code>%~dp1<\/code> is the folder of the first argument) and on <code>for<\/code> loop variables (<code>%%~dpF<\/code>).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Why %cd% is unreliable in real scripts<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Run as administrator:<\/strong> the current directory becomes <code>C:\\Windows\\System32<\/code>, so <code>%cd%<\/code> points there instead of your script folder.<\/li>\n\n\n<li><strong>Task Scheduler:<\/strong> unless you fill in &#8220;Start in&#8221;, tasks start in <code>C:\\Windows\\System32<\/code> as well.<\/li>\n\n\n<li><strong>Called from another script or a CI agent:<\/strong> <code>%cd%<\/code> is the caller&#8217;s working directory.<\/li>\n\n\n<li><strong>Double-clicking in Explorer<\/strong> starts in the script folder, which is why a script can work on your machine and fail elsewhere.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Recommended pattern<\/h2>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\">@<span class=\"hljs-keyword\">echo<\/span> off\nsetlocal\nrem Remember the script folder before anything can change %<span class=\"hljs-number\">0<\/span>\nset <span class=\"hljs-string\">\"SCRIPT_DIR=%~dp0\"<\/span>\n\nrem Work relative to the script folder, then <span class=\"hljs-keyword\">return<\/span> to where we started\npushd <span class=\"hljs-string\">\"%SCRIPT_DIR%\"<\/span>\n\n<span class=\"hljs-keyword\">echo<\/span> Script folder : %SCRIPT_DIR%\n<span class=\"hljs-keyword\">echo<\/span> Working folder: %cd%\n<span class=\"hljs-keyword\">if<\/span> exist <span class=\"hljs-string\">\"%SCRIPT_DIR%config.ini\"<\/span> <span class=\"hljs-keyword\">echo<\/span> Found config.ini next to the script\n\npopd\nendlocal<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<ul class=\"wp-block-list\">\n<li>Always quote paths: <code>\"%~dp0\"<\/code>. Without quotes the script breaks when the folder name contains spaces.<\/li>\n\n\n<li><code>%~dp0<\/code> already ends with a backslash, so write <code>\"%~dp0config.ini\"<\/code>, not <code>\"%~dp0\\config.ini\"<\/code>.<\/li>\n\n\n<li><code>pushd<\/code> also works when the script is on a network share (UNC path such as <code>\\\\server\\share<\/code>): it maps a temporary drive letter. <code>cd \/d<\/code> cannot make a UNC path the current directory.<\/li>\n\n\n<li>Save <code>%~dp0<\/code> into a variable at the top. A plain <code>shift<\/code> command also shifts <code>%0<\/code>, after which <code>%~dp0<\/code> no longer points to the script.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Equivalents in PowerShell and Bash<\/h2>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th><\/th><th>Script folder<\/th><th>Current directory<\/th><\/tr><\/thead><tbody><tr><td>Batch<\/td><td><code>%~dp0<\/code><\/td><td><code>%cd%<\/code><\/td><\/tr><tr><td>PowerShell<\/td><td><code>$PSScriptRoot<\/code><\/td><td><code>Get-Location<\/code> or <code>$PWD<\/code><\/td><\/tr><tr><td>Bash<\/td><td><code>\"$(cd \"$(dirname \"${BASH_SOURCE[0]}\")\" &amp;&amp; pwd)\"<\/code><\/td><td><code>$PWD<\/code> or <code>pwd<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Frequently asked questions<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Does %~dp0 work in .cmd files?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Yes. <code>.bat<\/code> and <code>.cmd<\/code> files are both run by <code>cmd.exe<\/code> and support the same modifiers.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Why does %~dp0 show the literal text at the command prompt?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Because <code>%0<\/code> only exists inside a running batch file. At the interactive prompt there is no script, so the text is not expanded.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How do I get the folder without the trailing backslash?<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">Store it and cut the last character: <code>set \"DIR=%~dp0\"<\/code> followed by <code>set \"DIR=%DIR:~0,-1%\"<\/code>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Related tutorials<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.devopsschool.com\/blog\/category\/programming\/shell-script\/\">All shell scripting tutorials<\/a><\/li>\n\n\n<li><a href=\"https:\/\/www.devopsschool.com\/blog\/category\/operating-systems\/windows\/\">All Windows tutorials<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>%~dp0 is the folder that contains the running batch file, while %cd% is the current working directory of the command prompt. They are the same only when&#8230; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_joinchat":[],"footnotes":""},"categories":[454],"tags":[26307],"class_list":["post-2560","post","type-post","status-publish","format-standard","hentry","category-shell-script","tag-batch-scripting"],"_links":{"self":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/2560","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/comments?post=2560"}],"version-history":[{"count":3,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/2560\/revisions"}],"predecessor-version":[{"id":79990,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/posts\/2560\/revisions\/79990"}],"wp:attachment":[{"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/media?parent=2560"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/categories?post=2560"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsschool.com\/blog\/wp-json\/wp\/v2\/tags?post=2560"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}