Strange behavior when running start.bat file in Windows 2019 Core - windows

I have a start.bat which does nothing but
#echo off
Echo "Hello World"
Then I open a cmd window and type in
start "" /D D:\Test start.bat
it opens a new cmd window with "Hello World", but does not close the window automatically.
Now I created another bat file named start2.bat which has the same content as the start.bat above. Then I type in
start "" /D D:\Test start2.bat
it opens a new cmd window with "Hello World", and does close the window automatically.
Why is it happening ???
I try to use some trace tool to analyse, from the tool, it seems like my system turn start "" /D D:\Test start.bat to cmd.exe /K start.bat
TraceTool
Tried in another computer, now, no matter which file name I used, it turns into cmd/exe /K start.bat
start3.bat
Update:
Turns out adding /C does not work for me, attach process tree below, the system still changes /C to /K
Process tree view with /C
Update:
Provide full process view. My start.bat is exactly the same as above
And now I open a cmd.exe window, and typed in
"c:\windows\system32\cmd.exe" /C START "test" /D "C:\test" start.bat or just cmd /C START "test" /D "C:\test" start.bat
And
"c:\windows\system32\cmd.exe" /C START "test" /D "C:\test" start2.bat or just cmd /C START "test" /D "C:\test" start2.bat
From the process tree view, it can be seen that the first command turns into
C:\windows\system32\cmd.exe /K start.bat
While the second command turns into
C:\windows\system32\cmd.exe /c "c:\test\start2.bat" "
cmd.exe
Full Process Tree View

From the usage information of the start command (excerpt from the description of the command/program argument):
[...]
command/program
If it is an internal cmd command or a batch file then
the command processor is run with the /K switch to cmd.exe.
This means that the window will remain after the command
has been run.
If it is not an internal cmd command or batch file then
it is a program and will run as either a windowed application
or a console application.
[...]
Hence when starting an internal command or a batch file, cmd.exe together with its /K option is used, so start "" "script.bat" is equivalent to:
start "" cmd.exe /K "script.bat"
If you want the new console window to become closed, use this instead:
start "" cmd.exe /C "script.bat"

Related

terminal window not closing after start command

My bat file is as follows:
start /B cmd /C "C:\Program Files\Microsoft Office\root\Office16\OUTLOOK.EXE"
timeout 60
start /B cmd /C "C:\Users\userfoo\AppData\Local\Microsoft\Teams\Update.exe --processStart ""Teams.exe"""
I have a shortcut placed in my desktop. After launching a command line window pops up and executes these commands. After the 2nd start command, the window should close, but it does not close.
Anything wrong with the syntax?
Edit: Actually, I commented out the 2nd and 3rd statements, and, did the exercise once more...I observed that the terminal window doesn't close.
You should use the exit command at the end. Your file should look something like this:
start /B cmd /C "C:\Program Files\Microsoft Office\root\Office16\OUTLOOK.EXE"
timeout 60
start /B cmd /C "C:\Users\userfoo\AppData\Local\Microsoft\Teams\Update.exe --processStart ""Teams.exe"""
exit

Windows Registry Run Key

According to http://msdn.microsoft.com/en-au/library/aa376977(v=vs.85).aspx:
"Run and RunOnce registry keys cause programs to run each time that a user logs on. The data value for a key is a command line."
Should I then be able to add a key with:
Name: MyName
Data: START /MIN "Title" "cmd.exe" /c "#echo off && "C:\TestApplication.exe" -Arg1 "Arg2"
with the goal being to start my console application "C:\TestApplication.exe" minimized with arguments "-Arg1 "Arg2"" when Windows starts?
I ask because I cannot seem to get it working.
The documentation is misleading, though not strictly incorrect. The command line is passed directly to CreateProcess() rather than being passed to cmd.exe so commands internal to cmd.exe, such as start, are not valid. This means you need to add cmd /c at the start of the command line.
You're also missing a quote mark at the end, and you don't need to put quotes around cmd.exe. This works:
cmd /c START /MIN "Title" cmd /c "#echo off && "C:\TestApp.exe" -Arg1 "Arg2""
However, since the target application is an executable, not a batch file, you don't need the #echo command either:
cmd /c START /MIN "Title" cmd /c ""C:\TestApp.exe" -Arg1 "Arg2""
(Note that the command line passed to /c is never echoed.)

How can use Windows cmd/bat file to open various apps at once and close the terminal windows?

I am using this:
start /b cmd /c "C:\Windows\notepad.exe"
start /b cmd /c "C:\Program Files\Google\Chrome\Application\chrome.exe"
start /b cmd /c "C:\Program Files\Skype\Phone\Skype.exe"
exit
The above works but keeps the prompt/terminal window open. How can I close it from inside the bat/cmd file as soon as the last app is open?
Try the following instead:
C:
cd \Windows
start notepad.exe
cd "\Program Files\Google\Chrome\Application"
start chrome.exe
cd "\Program Files\Skype\Phone"
start Skype.exe
exit
it is probably the fact that your starting cmd - just start the application

"start %comspec% /c script.cmd" vs "start cmd /C script.cmd"

What is the difference between following commands:
start %comspec% /c script.cmd
start cmd /C script.cmd
I need that cmd window for script.cmd should close automatically when script.cmd is finished.
%comspec% just points to cmd.exe, so both commands will do the same thing. Other than that /C is correct, this will close the command prompt after execution
The difference is that "%comspec%" should expand to the default command interpreter, whereas "cmd" searches for a cmd executable and invokes it. Most of the time they are the same.
But in case there is a cmd executable (cmd.exe, cmd.bat, etc) in the current directory, than that cmd executable will be invoked.
I would go with using %comspec%
For a not admin user on WindowsXP-SP2.
%comspec% starts in %WINDIR%\System32.
'cmd' starts in user's home e.g. C:\Document And Settings\USER1.

In Windows, how do I run a shortcut into the background from a command prompt?

cmd /C "myshortcut1.lnk"
cmd /C "myshortcut2.lnk"
Works, but gives me a pop-up DOS window which, when closed, kills my two loaded programs. Same is true for this:
start /B cmd /C "1.lnk"
start /B cmd /C "2.lnk"
start /B cmd /C "3.lnk"
start /B cmd /C "4.lnk"
Try start:
start myshortcut1.lnk
start myshortcut2.lnk
I do not have a Windows machine nearby to test right now, but should work.
Have you tried this?
C:\> my_shortcut.lnk

Resources