Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: %~dp0 vs %cd%  (Read 133199 times)

0 Members and 1 Guest are viewing this topic.

optix2000

    Topic Starter


    Rookie

  • Noobcake
    %~dp0 vs %cd%
    « on: April 01, 2008, 09:42:04 PM »
    Hello,

    Just wondering what's the difference in using %~dp0 or %cd% to find the current directory, since I'm not sure that either one will work in every case. Or do both work exactly the same?

    Thanks.

    Dias de verano

    • Guest
    Re: %~dp0 vs %cd%
    « Reply #1 on: April 02, 2008, 12:33:19 AM »
    They are not equivalent.

    %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)

    %~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's name.

    An experiment like the following shows the difference

    Here is D:\dirshow.bat:

    Code: [Select]
    @echo off
    echo this is %%cd%%  %cd%
    echo this is %%~dp0 %~dp0

    Run it from C:\ and this is what you see

    Code: [Select]
    C:\>D:\dirshow.bat
    this is %cd%  C:\
    this is %~dp0 D:\
    « Last Edit: April 02, 2008, 09:58:10 AM by Dias de verano »

    mplichta



      Newbie

      • Experience: Beginner
      • OS: Unknown
      UNC Paths
      « Reply #2 on: March 23, 2012, 09:59:01 AM »
      There is one more important distinction to note.  Normally the command prompt, and therefore cmd files, don't support UNC paths.

      However, using %~DP0 you can use relative paths to install software without mapping a drive.  The command below is what I use to install Adobe Design Premium and it works from any path, UNC, Flash drive, or mapped drive.

      msiexec /i "%~dp0\DP 32bit Basic\Build\DP 32bit Basic.msi" /q

      I use this by typing \\server\share\software\adobe CS5 into the start menu or run command (xp).  From there I can just double click a cmd file containing the command above. 

      patio

      • Moderator


      • Guru
      • Maud' Dib
      • Thanked: 1387
        • Yes
      • Experience: Beginner
      • OS: Windows 7
      Re: %~dp0 vs %cd%
      « Reply #3 on: March 23, 2012, 10:00:55 AM »
      It's been 4 years...
         
      "
      All generalizations are false, including this one.  "

      OnePostPony



        Newbie

        • Experience: Experienced
        • OS: Windows 7
        Re: %~dp0 vs %cd%
        « Reply #4 on: October 18, 2012, 06:51:08 AM »
        It's been 4 years...

        So what? I stumbled upon this thread today and found both replies useful.

        foxidrive



          Mentor
        • Thanked: 193
        • Experience: Experienced
        • OS: Windows 8
        Re: UNC Paths
        « Reply #5 on: October 18, 2012, 08:04:36 AM »
        There is one more important distinction to note.  Normally the command prompt, and therefore cmd files, don't support UNC paths.

        I don't think you are correct.

        This should work just fine

        Code: [Select]
        xcopy "\\server\share1\*.txt" "\\server\share2\"
        and this too:

        Code: [Select]
        pushd "\\server\share\software\adobe CS5"
        msiexec "\DP 32bit Basic\Build\DP 32bit Basic.msi" /q
        popd

        TechnoGeek



          Hopeful
        • Thanked: 36
          • Experience: Beginner
          • OS: Windows 7
          Re: %~dp0 vs %cd%
          « Reply #6 on: October 18, 2012, 08:14:39 AM »
          More specifically, cmd.exe (including executed .bat files, .cmd files, etc.) does not support the current/working directory to be a UNC path. pushd gets around this by assigning and switching to a drive letter that is mapped to the target path, and most other programs (commandline or not) work perfectly fine with a UNC path.

          patio

          • Moderator


          • Guru
          • Maud' Dib
          • Thanked: 1387
            • Yes
          • Experience: Beginner
          • OS: Windows 7
          Re: %~dp0 vs %cd%
          « Reply #7 on: October 18, 2012, 08:31:09 AM »
             
          "
          All generalizations are false, including this one.  "

          foxidrive



            Mentor
          • Thanked: 193
          • Experience: Experienced
          • OS: Windows 8
          Re: %~dp0 vs %cd%
          « Reply #8 on: October 18, 2012, 09:23:30 AM »
          It's been 4 years...

          I wonder if this topic will be dredged up again in 2016. ;)

          patio

          • Moderator


          • Guru
          • Maud' Dib
          • Thanked: 1387
            • Yes
          • Experience: Beginner
          • OS: Windows 7
          Re: %~dp0 vs %cd%
          « Reply #9 on: October 18, 2012, 09:35:40 AM »
          Judging by it's history...i'd say that's a safe bet...

          Seeya then...
             
          "
          All generalizations are false, including this one.  "