Search code examples
windowsbatch-fileworking-directory

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


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.


Solution

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

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