Arun-
Arun-

Reputation: 865

execute a command from a batch file from a different folder

I have 3 folders on same level (Folder 1,Folder 2 & Folder 3) inside a parent Folder named "MyApp" and my .bat file is in Folder 1. In Folder 2 I have my Angular app files and node server. Is there a way where I can write in my batch file to execute some commands ("npm start") the npm modules are in Folder 2. Something like this

echo "Starting App Module"
cd "\Folder 2"
start "" npm start 

I don't want to give the absolute path like this(below) which works fine.

echo "Starting App Module"
CD D:\MyApp\Folder 2 
start ""npm start

Please let me know your thoughts.

Upvotes: 1

Views: 1957

Answers (1)

Mofi
Mofi

Reputation: 49216

There are lots of possible solutions. I think, the best one for your task is:

echo "Starting App Module"
start "" "%~dp0..\Folder 2\npm.exe" start

%~dp0 ... references drive and path of batch file ending with a backslash. Run in a command prompt window call /? and read all help pages output for details.

Now it does not matter which directory is the current directory on start of the batch file. npm.exe is started with a path relative to path of the batch file independent on current working directory set for the batch file.

Upvotes: 2

Related Questions