1

In the answers at https://stackoverflow.com/questions/8055371/how-do-i-run-two-commands-in-one-line-in-windows-cmd, & "runs" one command, then the next, while && runs the 2nd command only if the first completes without error.

My problem is that it is not clear to me whether using & will wait for the first command to complete before running the second one. On the other hand && clearly will do so, but I want the 2nd command to run, synchronously, after the first one completes, regardless whether the first one succeeds.

Saying cmd "runs" the first one, then the second, to me only implies that each one is submitted in order; it says nothing about whether the second one is submitted only after the first one finishes.

Which is the actual behavior of &?

Thanks.

--peter

pbpb
  • 11
  • 2
  • 2
    Not as easy to answer as one would think. Technically: yes, it waits for the first command to finish before executing the second (`timeout 5 & echo done.`). When the first command happens to be a GUI program (for example Notepad.exe), this seems to change. Technically it does not: Notepad gets started as a detached process and the "command" itself is done (doesn't care about the detached process): `notepad test.txt & echo done.') – Stephan Jun 24 '23 at 21:45
  • 1
    Please read [single line with multiple commands using Windows batch file](https://stackoverflow.com/a/25344009/3074564). There must be differed between running executables by `cmd.exe` from within a command prompt window or during processing a batch file. The *Windows Command Processor* always waits for the self-termination of a started executable on processing a batch file. It does not matter in this case if the executed application is a Windows console or a Windows GUI application. The exit code is always assigned to __dynamic__ variable `ERRORLEVEL` for evaluation by more code in bat file. – Mofi Jun 25 '23 at 08:14
  • The `cmd` internal __START__ can be used to change the execution behavior of an executable on processing a batch file to be executed as separate, parallel running process. `cmd.exe` running as command prompt interpreter waits for self-termination of a Windows console applications. Most [Windows commands](https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/windows-commands) in `%SystemRoot%\System32` are such executables. But `cmd` starts from within a command prompt window a Windows GUI application as parallel running process without waiting for its self-close. – Mofi Jun 25 '23 at 08:18
  • 1
    The command __START__ with option `/W` can be used in a command prompt window to run a Windows GUI application with waiting for its self-termination before the command prompt becomes available again for the user, or run the next command in case of multiple commands are specified in one command line with usage of the command operators `&`, `&&`, `||`. That's it. Short: There is always waited for started executable terminated itself before running next command on executable started while a batch file is processed by `cmd` which is what is most likely most important for you. – Mofi Jun 25 '23 at 08:24

0 Answers0