Windows – Have the command prompt launch an application like Notepad and block until it is closed

batchscriptwindows

I have a Batch script I essentially want it to present the user with a notepad window opened with a configuration file for the user to edit each time they run the script, and I want the Command Prompt to wait until this program is closed to continue with execution, and run the program that uses this configuration file.

I want to do this because I don't want to take user input in the Batch script with the "set /P" command style, and have to set-up parsing reading and writing to that configuration file that has many values to inspect.

I believe the standard behavior on Windows when executing any executable program, is for "exe" files it will open the program in its own process and immediately control back to the script/command prompt. For "bat" and "cmd" script files, it will execute the script and wait until control is passed back to the first script or command prompt. This should be the behavior when at least calling the other script with the "CALL" command or calling it directly.

Now on Linux I believe the default behavior for executable content that is both native machine code that can come without an extension, and scripts like "sh" files, are executed in a blocking manner, unless you specify the ampersand ("&") character somewhere on the input line.

So I am trying to achieve the blocking behavior on Windows as it is more easily accomplished on Linux, and would like the closing of the program to trigger the original script to continue, or return control to the command prompt.

Best Answer

You could use either start command (note order of /B /WAIT switches)

start "" /B /WAIT notepad.exe "somefile"

or simply

notepad.exe "somefile"

As per above link:

When you run a 32-bit graphical user interface (GUI) application, cmd does not wait for the application to quit before returning to the command prompt. This behavior does not occur if you run the application from a command script.

Edit with regard to Karan's comment: Why is /B required?

/B starts an application without opening a new Command Prompt window.

  • To say the truth: if you run a graphical user interface application then no new Command Prompt window opens even thought /B switch is omitted.
  • On the other hand, if you run a command line tool, omitting the /B switch causes running that tool in a new Command Prompt window which will close immediately after it come to an end and all it's output is lost.

Hence, I use the /B /WAIT switches together as a matter of general principle: keep the same behaviour regardless of disposition of started program and spare from thinking about it...

Supplement on order of /B /WAIT switches with examples and more links from StackOverflow: start /WAIT /B doesn't work (the /wait argument is ignored) whilst start /B /WAIT works...