2

When I run a batch file that lies in C:\some\dir I know I can use %cd% and %~dp0 to get the path of the batch file itself and other information as described here.

But I have not managed to get the "present working directory" after the batch file has executed a couple of cd commands.

So for example:

  • Batch file is in C:\some\dir
  • Batch file executes cd sub and later cd sub2

How would I get the path to that folder?

It should yield C:\some\dir\sub\sub2 and would be the equivalent of pwd command when doing all of the above manually in the command line!

I had no luck with any of the commands above! It says here the %cd% variable is supposed to update after cd commands are used, but I always get C:\some\dir as output.

EDIT

link to a short version of my batch file

Community
  • 1
  • 1
bendulum
  • 1,777
  • 1
  • 13
  • 18
  • It's working exactly as you describe that it should, over here... – zb226 Apr 30 '15 at 09:22
  • 1
    Please read [here](http://stackoverflow.com/a/25440709/2861476). Without seeing your code (it is easier if you include it in the question), it seems there could be two options: 1) a problem with delayed expansion or 2) a value directly assigned to the `%cd%` variable masking the value. – MC ND Apr 30 '15 at 09:52

2 Answers2

3

If your Batch file does something like this:

@echo off
echo Start at: %cd%

cd sub
cd sub2

echo Currently at: %cd%

The second echo command will show C:\some\dir\sub\sub2 for sure. However, if the %cd% is placed in a code block, that is, enclosed in parentheses (like inside an if or for command), then this is the classical Delayed Expansion problem (as MC ND indicated):

@echo off
setlocal EnableDelayedExpansion

>log.txt (
   cd sub
   cd sub2

   echo Before this code block the current directory was: %cd%
   echo But currently it is: !cd!
)

For further details, look for "delayed expansion".

Aacini
  • 65,180
  • 12
  • 72
  • 108
  • I have edited my original question and added a link to my batch file. You were right regarding the **code block / delayed expansion**! `setlocal EnableDelayedExpansion`and using the `!cd!` command fixed it. Thank you! – bendulum Apr 30 '15 at 13:05
1

I) you can preserve the current directory at the begging of the script:

@echo off
set "curr_dir=%cd%"
...some cd commands...
cd /d "%curr_dir%" 

II) you can use pushd/popd (may be better)...

@echo off
....
pushd sub
...some commands...
popd
pushd sub2
...some commands...
popd

...or like this, if the given sub-directories might not exist:

@echo off
....
pushd .
cd /D sub
...some commands...
popd
pushd .
cd /D sub2
...some commands...
popd

Explanation: Supposing you successfully pushed sub to the directory stack but sub2 cannot be found so pushd sub2 fails and nothing is pushed, the next popd un-pushes sub unintentionally; pushing the current directory pushd . however won't ever fail...

more info

aschipfl
  • 33,626
  • 12
  • 54
  • 99
npocmaka
  • 55,367
  • 18
  • 148
  • 187
  • Regarding I) I'm not trying to go _back_ to the original directory. I'm trying to get the present directory _after_ the cd commands. Will try II) and report back. – bendulum Apr 30 '15 at 11:02
  • Altough they seem quite useful `pushd` and `popd` did not work in my case. – bendulum Apr 30 '15 at 13:01