Batch file : How to get current directory

by admin on October 23, 2011

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

{ 1 comment… read it below or add one }

Pat July 3, 2012 at 10:26 pm

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

Reply

Leave a Comment

HTML tags are not allowed.

Previous post:

Next post: