`pwd` in bat files

Ocassionally I need to automate things with Windows cmd or bat files. I can never seem to remember the cryptic Windows shorthand to get the working directory name and end up digging through old scripts to find it.

%~dp0 is the Windows equivalent of `pwd` in bash.


set PWD=%~dp0
echo %PWD%

Searching Google for %~dp0 turned up an interesting Microsoft link about batch parameters.

Advertisements
Posted in windows
12 comments on “`pwd` in bat files
  1. Jonas says:

    thanks :) you saved my life!

  2. Oleg says:

    Another great thank you :) terrible windows cmd shell :)

  3. Mathieu says:

    Thanks !!

  4. ed says:

    If you have command extensions enabled %CD% also works:

    C:\WINDOWS\system32>echo %CD%
    C:\WINDOWS\system32

    (a little easier to remember!)

    • katmoon says:

      %~dp0 is different from %CD%

      If you run a batch file from cmd,

      • %CD% returns current directory for cmd,
      e.g. C:\Documents and Settings\%USERNAME%

      • %~dp0 returns current directory for the executable file.
      E.g. your run the script from D:\myscripts\bla.bat, then %~dp0 will return D:\myscripts

      Thanks for the topic!

  5. Tobx says:

    Thanks a lot!!

    Those Batch-Stuff is really awfull, but your tip made it a bit more *nix. ;)

  6. Wolfgang says:

    THIS IS INCORRECT!

    As katmon already pointed out, the behaviour of the unix command pwd and the windows command cd are equivalent.

    %~dp0 only gives you %0

    This can be dangerous if you want to test whether the script is called with the correct working directory.

  7. letroll says:

    un merci de France pour l’information :-)

  8. gupta says:

    This is wrong. pwd is equivalent to %cd% (or .) If you want the current working directory, use %cd%. If you want the directory of the executable, use %~dp0.

    • donc says:

      The answer in the post is correct. I think you misunderstood the bash part.

      In a bash script `pwd` gives the directory of the executable just like %~dp0 in a cmd script.

      Note the backticks around the pwd. The backticks cause bash to execute pwd and replace `pwd` in the script with the standard output from the pwd command, which is the working directory for the script itself.

  9. Adam says:

    You are wrong donc. pwd in bash is print working directory. it gives the current directory. The backticks simply making it return the value instead of printing it, but it still gives the current directory. much like %cd% in windows batch files. The %~dp0 gives the directory of the script, not the working directory. The unix equivalent would be `dirname $0`

    You can even change directories within a script! %~dp0 doesn’t change. Nor does it reflect that you might have called the script from somewhere else

    The following windows batch file

    @echo off

    echo dp0: %~dp0
    echo cd: %cd%

    cd c:\Windows
    echo Changing to C:\Windows

    echo dp0: %~dp0
    echo cd: %cd%

    is equivalent to the following bash shell script


    echo \$0: `dirname $0`
    echo pwd: `pwd`

    cd /bin/
    echo Changing to /bin/

    echo \$0: `dirname $0`
    echo pwd: `pwd`

    If I run the first from H: as C:\Temp\foo.cmd I get the following output

    H:\>c:\Temp\foo
    dp0: c:\Temp\
    cd: H:\
    Changing to C:\Windows
    dp0: c:\Temp\
    cd: H:\

    If I run the linux version from my home folder as /smbshare/foo.sh I get the following output

    [adamc@adamc-centos ~]$ /smbshare/foo.sh
    $0: /smbshare
    pwd: /home/adamc
    Changing to /bin/
    $0: /smbshare
    pwd: /bin

    As you can see, %~dp0 is simply *not* equivalent to PWD but to `dirname $0`. %cd% is equivalent to `pwd`

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: