How to set the working directory of a command in a Windows batch file?

Louis Rhys picture Louis Rhys · Mar 7, 2012 · Viewed 96.6k times · Source

Let's say I have these commands:

Prog1.exe
D:\SomeDir\Prog2.exe
Prog3.exe

Now, say for the second line, I would like the working directory to be D:\SomeDir, but in Prog1.exe and Prog3.exe I want the default working directory (normally, where my .bat file is). If I try this

Prog1.exe
cd D:\SomeDir
D:\SomeDir\Prog2.exe
Prog3.exe

Apparently Prog3 will be executed in SomeDir, which is not what I want.

Answer

jeb picture jeb · Mar 7, 2012

You could use the pushd/popd commands (help with pushd /?)

Prog1.exe
Pushd D:\SomeDir
Prog2.exe
popd
Prog3.exe