≡ Menu

Batch file : How to get current directory

Here’s a question from a blog reader.

I need to write a batch script file which can traverse to different directories and do some operations on those directories. Once done, I need to come back to the original directory where the batch script started and do some more stuff. I need to get the initial starting directory and save it in a variable. My question is what’s the simple way to get the the directory from batch script.

Below is the answer for this question.

There is a very simple way to get the directory from a batch script file. CD environment variable stores the current directory of a command window session. Just run the command ‘echo %CD%’ and check it yourself.

C:\Users\windmdline>echo The current directory is %CD%
The current directory is C:\Users\wincmdline
12 comments… add one
  • Pat

    Another way to do what the reader wanted would be to use the pushd and popd commands to traverse directories like a stack.

  • guest


    outputs blank empty line

    • OMGguy

      Aww, Dude You have to Input some value after this command as –
      echo hello i am someone
      like this if you put, it will display the text – hello i am someone.
      Hope you understand.

  • Jack 6

    If you traverse the flag of the 7th decimal variable. The channel of corresponding flag will stay static in the field of the command to enable what you need

  • Keith Greenshields

    How to create a batch file that can organise files into specific folders that have been downloaded from the internet.

    Example; if I was to download a number of different file types such as a pdf, mp4,mp3, or an app, they would normally download into the download file by default. I would like to take the many different file types in the download folder and organise them into specific folders within my libraries by batching them. Can this be done?

    • admin

      You can download all the files into the default folder. Next, create a subfolder for each of the file type you have. Next, run the command below.

      forfiles /M * /c "cmd /c move @file @ext\
  • DustyG

    %~dp0 is the variable you want.
    It outputs the drive and path of the batch file.
    EX: C:\mydir\myfile.bat becomes C:\mydir\

    Here’s a MS reference, it works in thing other than Batch; like VBS SCCM, PS, shortcuts too.

  • Ray

    Works like a charm, thanks :)

  • Jay


  • nikos3194

    Thanks, this post has been very useful to me

  • Twisted_Code

    Is there also a way to get the path to the batch that’s currently running? What I really want to do is make sure the batch I’m running is running in its own directory in case I forget to update the “start in” I have in my task scheduler for this… otherwise if I move or copy the directory, it will either not start what it’s supposed to or (in the case of copying) potentially worse.

  • SmartFemale

    Why use a strange, impossible to remember thing like “%~dp0” when you can just use %cd% instead? Duh.

    dir %cd%\MySubDirs

Leave a Comment