The solution is using the string:
"start %ComSpec% /D /S /C \"(\"C:\\Program Files\\Python\\python.exe\" \"$file\" || set /p =Failed execution. Press Enter to exit...) && set /p =Successful execution. Press Enter to exit...\""
The usage of shell_cmd
results in starting in background without a visible console window cmd.exe
with option /c
and the command line specified in next string using the Windows kernel library function CreateProcess with an appropriate filled STARTUPINFO structure.
How the Windows command processor cmd.exe
interprets the argument string(s) after the option /c
is explained by the help output on running cmd /?
in a command prompt window. There are five conditions which define the general behavior on interpreting the argument string(s) after option /c
(or option /k
) and the first one is the usage of option /S
left to option /C
or /K
.
How the Windows command processor instance is started by Sublime Text 4 on using shell_cmd
cannot be controlled by the user. In this case is executed in the background:
C:\Windows\System32\cmd.exe /c start %ComSpec% /D /S /C "("C:\Program Files\Python\python.exe" "$file" || set /p =Failed execution. Press Enter to exit...) && set /p =Successful execution. Press Enter to exit..."
$file
is replaced by the file name string by Sublime Text 4 before starting the Windows command processor.
For that reason the command line is coded in a manner to work with cmd.exe /c
as used by Sublime Text 4. The free Sysinternals (Microsoft) tool Process Monitor can be used to see what happens in background when an executable starts another executable. After double clicking on a process in log of Process Monitor and selecting tab Process the Command Line can be seen used to start the process.
A visible console window is wanted in this case which is the reason why the internal command start
is used to start one more instance of cmd.exe
. The usage of start
results in using again the function CreateProcess
, but this time by the cmd
instance started by Sublime Text 4, with using a STARTUPINFO
filled to open a new console window by CreateProcess
on starting the executable.
The executable to start is cmd.exe
specified with its full qualified file name by referencing the value of the predefined Windows environment variable ComSpec
with %ComSpec%
which is expanded by first started cmd.exe
instance already to C:\Windows\System32\cmd.exe
on Windows being installed in C:\Windows
before executing command start
. ComSpec
is a system environment variable defined by default with %SystemRoot%\System32\cmd.exe
. So it would be also possible to replace %ComSpec%
by %SystemRoot%\\System32\\cmd.exe
in the command line string.
The second instance of cmd
should be started with ignoring the AutoRun
registry string value which does not exist by default in Windows registry. That is for security and makes the starting process of second cmd
instance a little bit faster. More information about option /D
and the AutoRun
registry string value can be read in help of Windows command processor output on running cmd /?
in a command prompt window.
The option /S
is used to explicitly define how the second cmd
instance should interpret the argument strings after the next option /C
which is for executing a command line and closing cmd
after finishing the command line execution.
The command line to execute contains the conditional operators &&
and ||
as explained in detail by single line with multiple commands using Windows batch file and twice an equal sign. For that reason it is highly recommended to enclose the entire command line string in "
to get these operators and the equal signs interpreted as literal characters by first cmd
instance started by Sublime Text 4 when it parses the argument strings after option /c
, i.e. the entire coded command line string.
The second cmd
instance removes the first and the last "
from the argument string after option /C
because of option /S
and interprets the remaining command line for execution.
In can be read in help of cmd
that a file name (or any other argument string) containing a space or one of these characters &()[]{}^=;!'+,`~
(and also <>|
not possible in a file name) must be enclosed in "
to be interpreted as literal character. C:\Program Files\Python\python.exe
contains a space character which is interpreted as argument string separator on not being inside a double quoted argument string. Therefore the full qualified file name of Python executable must be enclosed in "
to be interpreted as one argument string and not as the two argument strings C:\Program
and Files\Python\python.exe
.
The usage of ||
and &&
makes it additionally necessary that the execution of Python with the command set
executed on python.exe
terminating itself with an exit value not equal 0 are combined in a command block by using a pair of (
and )
to get the set
command after &&
executed only if python.exe
exited with value 0 for successful execution of the Python script.
For the special requirements caused by using ||
and &&
see:
So the second started cmd
instance with a visible console window runs the command line:
("C:\Program Files\Python\python.exe" "$file" || set /p =Failed execution. Press Enter to exit...) && set /p =Successful execution. Press Enter to exit...
The cmd
instance started first by Sublime Text 4 closes itself already after successful start of second cmd
instance by internal command start
. In other words the second cmd
executes python.exe
with CreateProcess
with a STARTUPINFO
to use the console window and the input, output and error streams of second cmd
while first cmd
exited itself already which means for Sublime Text 4 that the execution of the tool finished already although in real second cmd.exe
and python.exe
are still running.