2

I have the following script which launches multiple sessions of RDP connection files at once (colleted from a folder which only contains rdp files).

If I launch this from cmd prompt, it launches all sessions in parallel (which is what I want)

for /r %i in (*.rdp) do (mstsc %~nxi /f)

**while **if i run this script, it just launches the first session then waits for the relative process to end before running the second connection and so on.

for /r %%i in (*.rdp) do (mstsc %%~nxi /f)

What I'm doing wrong? Shouldn't it be the default behavior of batch to run all commands in parallel?

I've checked this but it doesn't address my exact scenario and it doesn't work anyhow as expected (e.g. START myBatchScript.bat doens't change the "waiting for process" behavior)

EDIT Added answer based on comments (Thanks to @Compo and @Modi)

  • 2
    Perhaps ```@for /r %i in (*.rdp) do @(start "" mstsc "%~nxi" /f)``` – Compo Nov 10 '22 at 12:31
  • thank you that works. also in this formulation `for /r %%i in (*.rdp) do (start "" mstsc %%~nxi /f)` – Deozzissimo Nov 10 '22 at 13:45
  • Please keep the doublequotes, they give protection for possible spaces and other problematic characters in the file names. – Compo Nov 10 '22 at 13:47
  • Thanks. Any idea on why the behavior differs between running it in cmd vs batch file? – Deozzissimo Nov 10 '22 at 14:10
  • Directly in [[tag:cmd]] you'd use ```for /r %i in (*.rdp) do @(start "" mstsc "%~nxi" /f)```. The difference being in a [[tag:batch-file]] the `%`, characters need escaping with another, i.e. `@for /r %%i in (*.rdp) do @(start "" mstsc "%%~nxi" /f)`. – Compo Nov 10 '22 at 15:17
  • notation is clear, thanks; with my question I meant if you have any idea why running `for /r %i in (*.rdp) do (mstsc %~nxi /f)` directly in cmd launches all rdp sessions at the same time while running `for /r %%i in (*.rdp) do (mstsc %%~nxi /f)` as batch files runs the sessions one at a time awaiting the closure of the previous one before running the subsequent. If you want to replicate this just create a folder with more than one txt file and try launching `for /r %i in (*.txt) do (notepad %~nxi)` from cmd vs `for /r %%i in (*.txt) do (notepad %%~nxi)` as a batch file – Deozzissimo Nov 10 '22 at 16:46
  • 1
    @Deozzissimo When a Windows __GUI__ executable like `%SystemRoot%\System32\mstsc.exe` (fully qualified file name recommended in batch file) is started directly from within a command prompt window, `cmd.exe` starts it as separate running process so that the user can switch back to the command prompt window, for example with __Alt+TAB__ and immediately execute the next internal command of `cmd` or another executable. – Mofi Nov 10 '22 at 20:41
  • 1
    @Deozzissimo Only if there is run a Windows __console__ executable from within a command prompt window, `cmd.exe` starts it with waiting for self-termination of this executable which is using the standard input/output/error streams of `cmd.exe` and so also the console window of `cmd`. For more information about starting an executable please read the Microsoft documentation of the Windows kernel library function [CreateProcess](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessw). – Mofi Nov 10 '22 at 20:42
  • 1
    @Deozzissimo The meaning of the word __batch__ in *__batch__* file is running one command/executable after the other. The Windows Command Processor starts on processing a batch file every executable independent on being an application with a graphical user interface or a console application with waiting for self-termination of the started executable before opening the batch file again, reading the next line(s), parsing the line(s), closing the batch file and executing the next line(s). The internal command `start` of `cmd.exe` is for changing the default behavior on starting an executable. – Mofi Nov 10 '22 at 20:47
  • 1
    @Deozzissimo `start /wait` can be used directly in a command prompt window to start a Windows GUI executable and let `cmd.exe` wait until the user exits the GUI application. Just `start` can be used in a batch file to start an executable (GUI or console application) to run it as separate process parallel to `cmd.exe` which immediately continues with batch file processing (the `for` loop in your case) after calling `CreateProcess`. If you run `start /?` in a cmd window, read the output usage help and have read the `CreateProcess` documentation as well as the documentation page for the ... – Mofi Nov 10 '22 at 20:50
  • 1
    @Deozzissimo ... [STARTUPINFO](https://learn.microsoft.com/en-us/windows/win32/api/processthreadsapi/ns-processthreadsapi-startupinfow) structure also carefully, you should notice that all options of `start` control parameters of `CreateProcess` or values in the `STARTUPINFO` structure. `CreateProcess` is used by nearly any other Windows executable capable starting another Windows executable like `explorer.exe`, `powershell.exe`, `cscript.exe` and `wscript.exe`, etc. The properties of a shortcut file (`.lnk`) are also more clear after reading the docs about `CreateProcess` and `STARTUPINFO`. – Mofi Nov 10 '22 at 20:54

1 Answers1

1

The coding solution to replicate the cmd behavior as a batch script is using the "start" command (as below, thanks to @Compo)

@for /r %i in (*.rdp) do @(start "" mstsc "%~nxi" /f)

for the reason why this happens refer to @Mofi comments.