Mark Needham

Thoughts on Software Development

Getting the current working directory from DOS or Batch file

with 15 comments

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.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Facebook
  • HackerNews
  • StumbleUpon
  • Twitter

Written by Mark Needham

August 12th, 2008 at 10:37 pm

Posted in Batch Scripting,Build

Tagged with ,

15 Responses to 'Getting the current working directory from DOS or Batch file'

Subscribe to comments with RSS

  1. 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

  2. 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

  3. Hi Thanks,Very useful info!

    Reshmi

    11 Dec 08 at 3:17 pm

  4. 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

  5. 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

  6. 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

  7. %~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

  8. 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

  9. 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

  10. 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

  11. Thanks @Adam. Your instructions helped to create custom print folder bat file.

    Amit Bhatia

    9 Apr 10 at 8:50 am

  12. For a complete list of the variable commands type for /? in cmd and they are all a few pages down.
    In addition, substitution of FOR variable references has been enhanced.
    You can now use the following optional syntax:

    %~I – expands %I removing any surrounding quotes (")
    %~fI – expands %I to a fully qualified path name
    %~dI – expands %I to a drive letter only
    %~pI – expands %I to a path only
    %~nI – expands %I to a file name only
    %~xI – expands %I to a file extension only
    %~sI – expanded path contains short names only
    %~aI – expands %I to file attributes of file
    %~tI – expands %I to date/time of file
    %~zI – expands %I to size of file
    %~$PATH:I – searches the directories listed in the PATH
    environment variable and expands %I to the
    fully qualified name of the first one found.
    If the environment variable name is not
    defined or the file is not found by the
    search, then this modifier expands to the
    empty string

    The modifiers can be combined to get compound results:

    %~dpI – expands %I to a drive letter and path only
    %~nxI – expands %I to a file name and extension only
    %~fsI – expands %I to a full path name with short names only
    %~dp$PATH:I – searches the directories listed in the PATH
    environment variable for %I and expands to the
    drive letter and path of the first one found.
    %~ftzaI – expands %I to a DIR like output line

    In the above examples %I and PATH can be replaced by other valid
    values. The %~ syntax is terminated by a valid FOR variable name.
    Picking upper case variable names like %I makes it more readable and
    avoids confusion with the modifiers, which are not case sensitive.

    Ben Luplow

    11 Jul 10 at 5:45 am

  13. Many thanks, it works!

    Agostino

    13 Jul 10 at 1:46 pm

  14. Thank you for your article. I also found these 2 commands below were good for finding the current directory and making it come up in Notepad, so you can copy and paste it:
    cd > filepath.txt
    filepath.txt | more

    Scott D

    7 Aug 10 at 3:47 am

  15. Still no way to get JUST the current directory name, not the full path! :(

    Mark

    20 Aug 10 at 8:53 pm

Leave a Reply