Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 857
Louis Rhys Avatar asked Mar 07 '12 06:03

Louis Rhys


People also ask

How do I change the working directory in a batch file?

Under Windows-10, go to All Apps, Windows System and the open the Command Prompt window. From the command prompt change directory to the batch file directory: cd \Tutorial\batch. Then type the name of the batch file test_conc followed by Enter.

How do I run a command in a specific folder?

If you want run command window from a specific folder. Click your folder then hold your shift key, then right click you will found option open command window here then click it.

What does @echo off do in a batch file?

Example# @echo off prevents the prompt and contents of the batch file from being displayed, so that only the output is visible. The @ makes the output of the echo off command hidden as well.


2 Answers

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

Prog1.exe Pushd D:\SomeDir Prog2.exe popd Prog3.exe 
like image 114
jeb Avatar answered Sep 25 '22 23:09

jeb


You could use the cd command (help with cd /?) with the %~dp0, batch file path, variable.

Prog1.exe cd D:\SomeDir Prog2.exe cd %~dp0 Prog3.exe 

For a complete list of %~ modifiers see call /? or for /? help.

However, I only add this as to provide a more complete answer on Stack Overflow. I would RECOMMEND using jeb's solution above.

like image 25
David Ruhmann Avatar answered Sep 24 '22 23:09

David Ruhmann