Computer Hope
Microsoft => Microsoft DOS => Topic started by: optix2000 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.
-
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:
@echo off
echo this is %%cd%% %cd%
echo this is %%~dp0 %~dp0
Run it from C:\ and this is what you see
C:\>D:\dirshow.bat
this is %cd% C:\
this is %~dp0 D:\
-
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.
-
It's been 4 years...
-
It's been 4 years...
So what? I stumbled upon this thread today and found both replies useful.
-
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
xcopy "\\server\share1\*.txt" "\\server\share2\"
and this too:
pushd "\\server\share\software\adobe CS5"
msiexec "\DP 32bit Basic\Build\DP 32bit Basic.msi" /q
popd
-
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.
-
It's been 4 years...
-
It's been 4 years...
I wonder if this topic will be dredged up again in 2016. ;)
-
Judging by it's history...i'd say that's a safe bet...
Seeya then...
-
Not quite 2016 yet, but I found this very helpful.
-
It's been 4 years...
Batch is slow... ::)
-
I wonder if this topic will be dredged up again in 2016. ;)
Close enough.
-
This thread helped me out with a batch script at the end of last year. Thank you so much!
-
I wonder if this topic will be dredged up again in 2016. ;)
Sonofagun....you were correct.... :P
-
LOL Like the Matrix Oracle ...would you like a cookie 8)
-
It's had a good run - but I might be 6 foot under in 2020 ;)
-
I'm just here for 2017, don't mind me.
-
I wonder if this topic will be dredged up again in 2016. ;)
Even after 4 years...... :P :D :D
-
*** sigh ***
-
It's had a good run - but I might be 6 foot under in 2020 ;)
RIP my friend.
-
As the former Dias de Verano, can I say (a little early) happy 10th anniversary to this thread!
-
Tell good 'ol Diaz i said Hi...... :P
-
He says hola, mi amigo!
-
;D
-
RIP my friend.
When did he pass away?
-
Thursday Nov 3rd 2016.
Don's Topic (https://www.computerhope.com/forum/index.php/topic,158835.0.html)
Dostips Topic (https://www.dostips.com/forum/viewtopic.php?f=3&t=7529&p=50015#p50015)
-
When did he pass away?
Shortly before you said how sad it was.
-
Foxidrive
passed away on Thursday 3rd November 2016
My memory is fading. :-[
Could somebody put a note in his profile?
-
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
-
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
-
RIP...
-
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.
-
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!!
-
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.
-
%~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.
-
Somehow i knew in the back of my mind it would be Dias ressurecting this one...
Who is that masked man ? ?
-
guys help me out? (https://get-mobdro.com) (https://get-tubemate.com)
You are not the "former Dias de Verano".
-
Hey Mike, hope all is well. Good to see you back.
-
We can forget about 2020 right
-
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
@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%