Stas Mackarow
Stas Mackarow

Reputation: 195

How to run .EXE from .BAT inside the folder where .EXE is locating

To recreate my problem you need to understand that I have next files in 2 folders.

K:\Script.bat

K:\Project\PortChanger.exe

K:\Project\settings.xml

I want to launch PortChanger.exe using Script.bat that contains next line:

start "K:\Project\PortChanger.exe"

This script is actually executing Program.exe, but my program throws me exception since PortChanger.exe can't find Settings.xml.

How can I launch PortChanger.exe from "K:\Project\", not from "K:\"? Now it seems like .BAT taking .EXE code and just running it where .BAT is locating.

To make it even more clear: enter image description here enter image description here enter image description here

Upvotes: 1

Views: 2658

Answers (2)

Compo
Compo

Reputation: 38708

You could use Start with its /D option:

Start "" /D "K:\Project" "K:\Project\PortChanger.exe"

Open a Command Prompt window and enter start /? to read its usage information.

Upvotes: 2

user7818749
user7818749

Reputation:

I would suggest you rather use pushd and popd

@echo off
pushd "K:\Project"
start "" PortChanger.exe
popd

pushd will change to the directory, launch the executable from it, then popd will return to the previous directory stored.

Upvotes: 1

Related Questions