How to change to a subdirectory and run an exe using a bat file in a Windows 7 system?

11,435

Solution 1

Just indicate to start command what program to start and what should be the starting folder for it.

Without the cd command, it can be written as

start "" /d "%~dp0my_subdirectory" "my_application.exe"

if the my_application.exe is located in the subdirectory, or

start "" /d "%~dp0my_subdirectory" "%~dp0my_application.exe"

if the application is located in the same folder as the batch file.

start command take the first quoted parameter as the title for the new process. To avoid problems, a empty string ("") is included in command as the title.

Solution 2

Try:

cd /d "%~dp0my_subdirectory"
start "" my_application.exe

or just:

start "" "%~dp0my_subdirectory\my_application.exe"
Share:
11,435
alwbtc
Author by

alwbtc

Updated on July 20, 2022

Comments

  • alwbtc
    alwbtc almost 2 years

    Using a bat file, I want to change to a sub directory of folder which bat file is in, and run my_application.exe in that directory,

    I try:

    cd /d %cd%\my subdirectory
    START %~dp0my_application.exe
    

    But it doesn't work, it says it can't find my_application.exe

  • foxidrive
    foxidrive about 10 years
    The first start command using /d specifies the working directory so the executable doesn't require a full path to be specified. That will simplify the command.
  • MC ND
    MC ND about 10 years
    @foxidrive, changed. Thank you.
  • JoeCodeCreations
    JoeCodeCreations almost 8 years
    I've been looking for this solution for awhile. This solved my problem. Thank you very much!