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 464278 times)

0 Members and 1 Guest are viewing this topic.

optix2000

  • Guest
%~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


    • Genius
    • Maud' Dib
    • Thanked: 1769
      • 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...
    " Anyone who goes to a psychiatrist should have his head examined. "

    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



        Specialist
      • Thanked: 268
      • 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

      • Guest
      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


      • Genius
      • Maud' Dib
      • Thanked: 1769
        • Yes
      • Experience: Beginner
      • OS: Windows 7
      Re: %~dp0 vs %cd%
      « Reply #7 on: October 18, 2012, 08:31:09 AM »
      " Anyone who goes to a psychiatrist should have his head examined. "

      foxidrive



        Specialist
      • Thanked: 268
      • 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


      • Genius
      • Maud' Dib
      • Thanked: 1769
        • 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...
      " Anyone who goes to a psychiatrist should have his head examined. "

      rsf722



        Newbie

        • Experience: Experienced
        • OS: Windows 8
        Re: %~dp0 vs %cd%
        « Reply #10 on: September 21, 2015, 12:33:00 PM »
        Not quite 2016 yet, but I found this very helpful.

        Geek-9pm


          Mastermind
        • Geek After Dark
        • Thanked: 1026
          • Gekk9pm bnlog
        • Certifications: List
        • Computer: Specs
        • Experience: Expert
        • OS: Windows 10
        Re: %~dp0 vs %cd%
        « Reply #11 on: September 21, 2015, 12:57:14 PM »
        It's been 4 years...
        Batch is slow...  ::)

        Squashman



          Specialist
        • Thanked: 134
        • Experience: Experienced
        • OS: Other
        Re: %~dp0 vs %cd%
        « Reply #12 on: September 21, 2015, 01:08:37 PM »
        I wonder if this topic will be dredged up again in 2016. ;)
        Close enough.

        Fighthouse



          Newbie

          • Experience: Expert
          • OS: Windows 7
          Re: %~dp0 vs %cd%
          « Reply #13 on: January 04, 2016, 07:44:41 AM »
          This thread helped me out with a batch script at the end of last year. Thank you so much!

          patio

          • Moderator


          • Genius
          • Maud' Dib
          • Thanked: 1769
            • Yes
          • Experience: Beginner
          • OS: Windows 7
          Re: %~dp0 vs %cd%
          « Reply #14 on: January 04, 2016, 08:00:02 AM »
          I wonder if this topic will be dredged up again in 2016. ;)

          Sonofagun....you were correct.... :P
          " Anyone who goes to a psychiatrist should have his head examined. "

          DaveLembke



            Sage
          • Thanked: 662
          • Certifications: List
          • Computer: Specs
          • Experience: Expert
          • OS: Windows 10
          Re: %~dp0 vs %cd%
          « Reply #15 on: January 04, 2016, 08:11:08 AM »
          LOL Like the Matrix Oracle ...would you like a cookie  8)

          foxidrive



            Specialist
          • Thanked: 268
          • Experience: Experienced
          • OS: Windows 8
          Re: %~dp0 vs %cd%
          « Reply #16 on: January 05, 2016, 03:01:11 AM »
          It's had a good run - but I might be 6 foot under in 2020 ;)

          Nathansswell



            Rookie

            Thanked: 1
            • Yes
            • I make music!
          • Certifications: List
          • Computer: Specs
          • Experience: Beginner
          • OS: Windows 8
          Re: %~dp0 vs %cd%
          « Reply #17 on: February 25, 2017, 02:37:19 PM »
          I'm just here for 2017, don't mind me.

          Ciyar



            Newbie

            • Experience: Beginner
            • OS: Unknown
            Re: %~dp0 vs %cd%
            « Reply #18 on: March 15, 2018, 03:37:10 AM »
            I wonder if this topic will be dredged up again in 2016. ;)

            Even after 4 years...... :P :D :D

            patio

            • Moderator


            • Genius
            • Maud' Dib
            • Thanked: 1769
              • Yes
            • Experience: Beginner
            • OS: Windows 7
            Re: %~dp0 vs %cd%
            « Reply #19 on: March 17, 2018, 11:18:04 AM »
            *** sigh ***
            " Anyone who goes to a psychiatrist should have his head examined. "

            Squashman



              Specialist
            • Thanked: 134
            • Experience: Experienced
            • OS: Other
            Re: %~dp0 vs %cd%
            « Reply #20 on: March 17, 2018, 10:03:07 PM »
            It's had a good run - but I might be 6 foot under in 2020 ;)
            RIP my friend.

            Salmon Trout

            • Guest
            Re: %~dp0 vs %cd%
            « Reply #21 on: March 18, 2018, 04:25:23 AM »
            As the former Dias de Verano, can I say (a little early) happy 10th anniversary to this thread!

            patio

            • Moderator


            • Genius
            • Maud' Dib
            • Thanked: 1769
              • Yes
            • Experience: Beginner
            • OS: Windows 7
            Re: %~dp0 vs %cd%
            « Reply #22 on: March 18, 2018, 07:16:13 AM »
            Tell good 'ol Diaz i said Hi...... :P
            " Anyone who goes to a psychiatrist should have his head examined. "

            Salmon Trout

            • Guest
            Re: %~dp0 vs %cd%
            « Reply #23 on: March 18, 2018, 07:19:25 AM »
            He says hola, mi amigo!

            patio

            • Moderator


            • Genius
            • Maud' Dib
            • Thanked: 1769
              • Yes
            • Experience: Beginner
            • OS: Windows 7
            Re: %~dp0 vs %cd%
            « Reply #24 on: March 18, 2018, 09:07:43 AM »
             ;D
            " Anyone who goes to a psychiatrist should have his head examined. "

            Geek-9pm


              Mastermind
            • Geek After Dark
            • Thanked: 1026
              • Gekk9pm bnlog
            • Certifications: List
            • Computer: Specs
            • Experience: Expert
            • OS: Windows 10
            Re: %~dp0 vs %cd%
            « Reply #25 on: March 18, 2018, 02:16:42 PM »
            RIP my friend.
            When did he pass away?

            BC_Programmer


              Mastermind
            • Typing is no substitute for thinking.
            • Thanked: 1140
              • Yes
              • Yes
              • BC-Programming.com
            • Certifications: List
            • Computer: Specs
            • Experience: Beginner
            • OS: Windows 11
            Re: %~dp0 vs %cd%
            « Reply #26 on: March 18, 2018, 02:24:30 PM »
            Thursday Nov 3rd 2016.

            Don's Topic

            Dostips Topic
            I was trying to dereference Null Pointers before it was cool.

            Salmon Trout

            • Guest
            Re: %~dp0 vs %cd%
            « Reply #27 on: March 18, 2018, 02:55:05 PM »
            When did he pass away?
            Shortly before you said how sad it was.

            Geek-9pm


              Mastermind
            • Geek After Dark
            • Thanked: 1026
              • Gekk9pm bnlog
            • Certifications: List
            • Computer: Specs
            • Experience: Expert
            • OS: Windows 10
            Re: %~dp0 vs %cd%
            « Reply #28 on: March 18, 2018, 08:51:50 PM »
             Foxidrive
            Quote
            passed away on Thursday 3rd November 2016
            My memory is fading.    :-[
            Could somebody put a note in his profile?

            Sefie



              Newbie

              • Experience: Expert
              • OS: Windows 10
              Re: %~dp0 vs %cd%
              « Reply #29 on: November 06, 2019, 01:34:44 AM »
              It's had a good run - but I might be 6 foot under in 2020 ;)
              Not quite 2020, yet, but I registered in late 2019 just to say that this thread was useful to me. ;D

              djshoxxx



                Newbie

                • Experience: Experienced
                • OS: Windows 7
                Re: %~dp0 vs %cd%
                « Reply #30 on: December 18, 2019, 01:40:12 AM »
                I was trying to understand what @pushd "%~dp0" ment, I googled it, and now I understand it because of this thread. Only 14 days from 2020 lol!

                thank you, contributors to this post, and a posthumous thank you and RIP to foxidrive

                patio

                • Moderator


                • Genius
                • Maud' Dib
                • Thanked: 1769
                  • Yes
                • Experience: Beginner
                • OS: Windows 7
                Re: %~dp0 vs %cd%
                « Reply #31 on: December 18, 2019, 05:56:01 AM »
                RIP...
                " Anyone who goes to a psychiatrist should have his head examined. "

                croger1998



                  Newbie
                  • Experience: Expert
                  • OS: Mac OS
                  Re: %~dp0 vs %cd%
                  « Reply #32 on: December 19, 2019, 08:11:48 AM »
                  I have been around PCs all my life. I have been in IT for 20+ years and have never came across %~dp0 before. No idea why. I am glad I came across this post to better understand this hidden gem.

                  Thanks for the info everyone.


                  martosio



                    Newbie

                    • Experience: Beginner
                    • OS: Unknown
                    Re: %~dp0 vs %cd%
                    « Reply #33 on: December 31, 2019, 07:27:02 PM »
                    This thread helped me about two years ago (august 2018 @ WTD), but due to thread policy I head to wait until 2020 to reply ;)

                    Thanks guys!!

                    Caldor



                      Newbie

                      • Experience: Experienced
                      • OS: Windows 10
                      Re: %~dp0 vs %cd%
                      « Reply #34 on: August 05, 2020, 07:54:58 AM »
                      It's been 4 years...
                      It's been... another 4 years...  ;D

                      I using a MiSTer FPGA to run a DOS core, and looking into batch scripting, but just cannot figure out how to get the path to the currently active directory.

                      I cannot get %~dp0 to work either... but trying to experiment some more. I am trying with FreeDOS, DOS 6.22 and DOS 7.1. But I guess I have to make sure I did try all of them.

                      BC_Programmer


                        Mastermind
                      • Typing is no substitute for thinking.
                      • Thanked: 1140
                        • Yes
                        • Yes
                        • BC-Programming.com
                      • Certifications: List
                      • Computer: Specs
                      • Experience: Beginner
                      • OS: Windows 11
                      Re: %~dp0 vs %cd%
                      « Reply #35 on: August 05, 2020, 01:51:29 PM »
                      %~dp0 and things like %cd% are part of the NT Extensions that were added to the command prompt with Windows NT4.

                      the "cd" command by itself outputs the current directory, but I don't know a way you can get that into an environment variable without utilizing things like QBASIC.
                      I was trying to dereference Null Pointers before it was cool.

                      patio

                      • Moderator


                      • Genius
                      • Maud' Dib
                      • Thanked: 1769
                        • Yes
                      • Experience: Beginner
                      • OS: Windows 7
                      Re: %~dp0 vs %cd%
                      « Reply #36 on: September 10, 2020, 05:17:16 PM »
                      Somehow i knew in the back of my mind it would be Dias ressurecting this one...

                      Who is that masked man ? ?
                      " Anyone who goes to a psychiatrist should have his head examined. "

                      Sam_Weller



                        Starter

                        • Experience: Beginner
                        • OS: Unknown
                        Re: %~dp0 vs %cd%
                        « Reply #37 on: September 30, 2020, 11:51:58 AM »

                        guys help me out?

                        You are not the "former Dias de Verano".

                        Allan

                        • Moderator

                        • Mastermind
                        • Thanked: 1260
                        • Experience: Guru
                        • OS: Windows 10
                        Re: %~dp0 vs %cd%
                        « Reply #38 on: September 30, 2020, 05:34:06 PM »
                        Hey Mike, hope all is well. Good to see you back.

                        Nathansswell



                          Rookie

                          Thanked: 1
                          • Yes
                          • I make music!
                        • Certifications: List
                        • Computer: Specs
                        • Experience: Beginner
                        • OS: Windows 8
                        Re: %~dp0 vs %cd%
                        « Reply #39 on: November 14, 2021, 02:50:16 PM »
                        We can forget about 2020 right

                        Sam_Weller



                          Starter

                          • Experience: Beginner
                          • OS: Unknown
                          Re: %~dp0 vs %cd%
                          « Reply #40 on: November 27, 2021, 02:40:05 AM »
                          It's been... another 4 years...  ;D

                          I using a MiSTer FPGA to run a DOS core, and looking into batch scripting, but just cannot figure out how to get the path to the currently active directory.

                          I cannot get %~dp0 to work either... but trying to experiment some more. I am trying with FreeDOS, DOS 6.22 and DOS 7.1. But I guess I have to make sure I did try all of them.

                          Resurrection time!

                          Works with MS-DOS 7 and I have no reason to think it won't work with (say) 6.22

                          Code: [Select]
                          @echo off
                          >temp1.bat echo @PROMPT SET _CD=$P
                          >temp2.bat command /c temp1.bat
                          call temp2.bat
                          del temp1.bat
                          del temp2.bat
                          echo currentDir=%_CD%