Getting the current working directory from DOS or Batch file
In the world of batch files I've been trying for ages to work out how to get the current/present working directory to make the batch script I'm working on a bit more flexible.
In Unix it's easy, just call 'pwd' and you have it. I wasn't expecting something that simple in Windows but it is! A call to 'cd' is all that's needed. If you need to set it in a batch script the following line does the trick:
|
|
set WORKING_DIRECTORY=%cd% |
I was surprised that something so simple (I do now feel like an idiot) wasn't easier to find on Google. I ended up going via Experts Exchange (how they end up with such high search results when you have to pay to see the information is beyond me) and several other verbose ways of solving the problem before finally coming across this article which explained it.
You don't have to pay to view the Experts Exchange answers, you just have to scroll past all the bogus "Subscribe to see this answer" boxes
It's tedious, but it's all there at the bottom
HTH
Andy
12 Aug 08 at 11:57 pm
Oh yeah! Wow I totally didn't realise that was the case. I always saw the massive 'Sign up now to view this solution' and immediately pressed the back button or just didn't bother going to the page in the first place.
Thanks for that though, useful to know.
Mark Needham
13 Aug 08 at 12:03 am
Hi Thanks,Very useful info!
Reshmi
11 Dec 08 at 3:17 pm
Before knowing what Andy wrote, I was super annoyed with Experts Exchange because they come up on the top of Google searches. I was wishing there was a default Google filter I could set, e.g. -site:"Experts Exchange", but no more!
Mark, Thanks for the tip on the Current Working Directory for DOS.
David
17 Dec 08 at 10:43 am
Everywhere its always the same, I've searched and searched using every possible wording I can think up. The above is all that ever comes up or some variant thereof. All I want returned or used in my batch file is just the folder name itself not the whole damn path.
I have been trying in vain to create a simple batch file that will start at whichever directory I execute it in and search through all it's subdirectories for any files and rename them all to match the name of the folder they reside in. I already know how to set up everything to rename all the files in all the directories sequentially.
I just can't believe xp's cmd.exe doesn't have a simple way of taking just the folder name itself [not the whole path] and making it available as a string to use for whatever, like providing a prefix for renaming files in this case. Gawd this is frustrating! It is so damn simple in UNIX. Why does XP have to be so friggan difficult???
anonymous
5 Jan 09 at 7:10 pm
I guess you are after a cheat sheet…
Ok… you owe me one.
If you are looking for the current working directory, you can use %cd%.
Alternatively, you can try tackling the problem in a different way by passing the directory you want to use in as an argument and referencing it with %1
For example:
MyBatchFileWithAPath.bat "C:\temp"
Also, if you are after the directory where the script lives, try %~dp0
(obvious isn't it?)
Finally, if you want to know the full path of the batch file, it is the first argument (index 0), ie %0
Try writing the following batch file:
echo Current dir = %CD%
echo Script dir = %~dp0
echo Batch path = %0
echo User argument 1 = %1
As for why it has to be so difficult, I'm not sure I can answer that one
Adam Pond
7 Apr 09 at 12:00 pm
%~dp0
That is what I have been looking for! Thank you so much! Why the ef was that so convoluted and hidden from any sort of help menu?
Ben Luplow
19 Feb 10 at 5:47 am
Now when our corp changes the directory paths all around I won't have to edit 200 scripts!
Ben Luplow
19 Feb 10 at 5:48 am
What Adam posted let me to this additional (obvious -NOT) piece of info:
For the drive where the batch file lives, use %~d0
For just the directory path where the batch file lives, use %~p0
i.e.
echo Current Drive = %~d0
echo Current Path = %~p0
Ron Patrick
27 Feb 10 at 1:54 pm
I always loved DOS-batch because it provided open-ended possibilities. I came from UNIX world and so appreciated its openness of interface ( not everything was wrapped tight in a GUI box). I have been using DOS for a while. I think though these days biterscripting has been replacing DOS pretty quickly. There are some excellent sample scripts posted at http://www.biterscripting.com. I still reamain a fan of DOS, but, must admit, tend to use biterscripting more and more these days. Also, sometimes, DOS just does not provide the functionality that I need, such as, redirecting the output to a string variable, easy date calculation, string parsing, stream editors, etc. That's when I have no choice but to use biterscripting.
SenHu
16 Mar 10 at 4:49 pm