Search code examples
windowsbatch-filecmdscheduler

How start a program via command line after the previous process terminated itself?


I run a program like notepad.exe via command line.

After the process terminated itself after completion of task of the started application, I need to run another program via command line, for example winword.exe.

So I would like a behavior like a scheduler, waiting in background up to completion of first process and then initiate start of another process.

Can I achieve this in Windows? And if yes, how?


Solution

  • Batch file solutions

    For the example Notepad.exe and Winword.exe the solution is quite simple with following batch file:

    @echo off
    %SystemRoot%\Notepad.exe
    start Winword.exe
    

    Windows command interpreter cmd.exe first starts Windows Notepad and halts execution of the batch file until Notepad.exe terminated itself which means the user

    • pressed Alt+F4 on keyboard, or
    • clicked on X symbol on right side of title bar of Notepad window, or
    • clicked in menu File on menu item Exit, or
    • double clicked on application symbol on left side of title bar of Notepad window, or
    • clicked once on application symbol on left side of title bar of Notepad window and clicked next on last application context menu item Close.

    Then internal command START of cmd.exe is used to start Microsoft Word in a separate process parallel to running command process. For that reason cmd.exe continues the batch file processing immediately after executing START while Microsoft Word is running parallel and exits because there is no more command line.

    The application to start must be specified usually with full path if the directory containing the executable is not included in environment variable PATH, enclosed in double quotes if the path contains a space or one of these characters &()[]{}^=;!'+,`~. Please take a look on What is the reason for '...' is not recognized as an internal or external command, operable program or batch file? for details on how Windows command interpreter finds executables and scripts specified on command line or in a batch file without path (and without file extension). So best would be as second line something like:

    start "" "%ProgramFiles(x86)%\Microsoft Office\Office14\Winword.exe"
    

    This command line starts 32-bit Microsoft Word 2010 installed in standard installation directory on a computer running 64-bit Windows. The additional empty argument string "" is necessary because command START would interpret otherwise the full qualified name of application to start enclosed in double quotes as title for a new console window. So the command START would start a new command process with the title C:\Program Files (x86)\Microsoft Office\Office14\Winword.exe for the console window without specifying explicitly an empty title with "" as first argument.

    Why is using just start Winword.exe working?

    The directory containing Winword.exe is not included in environment variable PATH. But as long as Winword.exe is installed at all, this command line nevertheless results in starting Microsoft Word. The reason is correct registration of application Winword.exe in Windows registry during installation. For that reason the command START is capable to find out where Winword.exe is installed and execute it. For details on how this works see answer on Where is “START” searching for executables?

    The three lines in batch file can be also optimized to a single line with multiple commands:

    @%SystemRoot%\Notepad.exe & start Winword.exe
    

    But this single command line can't be used directly in a command prompt window because of cmd.exe executes in this case Windows Notepad and Microsoft Word parallel.


    Command line solutions

    The command line solution for usage directly from within a command prompt window is:

    start /wait Notepad.exe & start Winword.exe
    

    This starts Windows Notepad in a separate process using command START with explicitly waiting for termination of Notepad.exe because of using additionally the START parameter /wait before one more START is executed to start Microsoft Word. There is no need of "" as empty title string here because of no argument string in this command line is enclosed in double quotes.

    But this command line solution has one disadvantage: the command prompt window can't be used further as long as Windows Notepad is running.

    So better would be starting from within current command prompt window a new command process with minimized window which first executes Windows Notepad, halts command line execution until Notepad terminates itself, then starts Microsoft Word and exits immediately after starting Winword.exe. This can be done with following command line:

    start "Notepad & Winword" /min cmd.exe /C "start /wait Notepad.exe & start Winword.exe"
    

    This command line results in starting cmd.exe as separate process, with a console window being minimized because of option /min of command START, with a console window title Notepad & Winword, which closes itself because of option /C of CMD, after first starting Notepad.exe and waiting for Notepad termination before starting Winword.exe not waiting for termination.

    Well, the additional, minimized console window with title Notepad & Winword is in real of no usage for the user. Therefore better would be the command line:

    start "" /B cmd.exe /C "start /wait Notepad.exe & start Winword.exe"
    

    The additional command process is started in this case with no window (in background) because of usage of option /B of command START. An empty title is specified here as no console window is shown at all.

    For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.

    • cmd /?
    • echo /?
    • start /?