4

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.

6

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...

2
  • 1
    Why is /B required? – Karan May 2 '15 at 1:28
  • Good addition on /B /WAIT behavior. Has caught me out a couple of times in the past before I realized the order mattered. – Tonny May 2 '15 at 9:54
1
notepad.exe | echo > NUL

What this does is, it opens notepad.exe and all of its output is redirected to the command echo > NUL. In order to redirect the whole output, the program must finish. Therefore we have to wait until notepad.exe is finished. The output goes to echo. Echo doesn't use the input in any way and just outputs something to NUL. NUL is a standard windows stream that goes nowhere.

3
  • At least with Notepadd++ the command prompt returns immediately, redirection nonwithstanding. – Peter - Reinstate Monica Sep 5 '17 at 12:41
  • @PeterA.Schneider - yes, that's the point of the pipe. Normally a windowed application returns to the command line immediately. The pipe to echo makes it wait until the program finishes. Which is exactly what Pysis asked. – Valentin Oct 14 '17 at 20:52
  • I wanted to upvote after verifying that it does work even with Notepad++ but had to edit first. Feel free to roll that back if you don't like it. I'll upvote as soon as the edit was reviewed. – Peter - Reinstate Monica Oct 15 '17 at 7:04
0

When writing the question, one answer came to me, although it does not achieve the exact result I intended.

It would be to open whatever program I wanted to open, and add the "pause" command directly after that one on the line below. It would pause script execution enough until the user closes the new program window that just appeared, and would need to realize they need to manually advance the script from that point. Pause does print out a prompt explaining what is happening to potentially inform the user ("Press any key to continue . . .").

This is not a major problem unless the script is minimized for whatever reason, and the user believes it has finished. It also isn't great because it does not automatically resume running the rest of the script when the program is closed, which is the effect I want.

0

Simplistic answer is in batch script or prompt:

cmd /c notepad.exe somefile

1
  • That doesn't give the intended behavior. Notepad is detached from the 2nd instance of CMD which immediately exits to the first CMD which will happily continue without waiting for notepad. – Tonny May 2 '15 at 9:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.