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 }
Another way to do what the reader wanted would be to use the pushd and popd commands to traverse directories like a stack.