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
Related
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"
I am trying to run a batch file with start /high and still get the return/exit code, i.e. %ERRORLEVEL%. The problem seems to be that command START does not return the exit code that the batch file returns.
We have a simple batch file for testing named BatFileThatReturnsOne.bat.
The contents of BatFileThatReturnsOne.bat are
EXIT /B 1
We are calling this as such:
start /high /wait BatFileThatReturnsOne.bat
But no matter what the batch file returns, the execution of start never has a %ERRORLEVEL% of anything other than 0 (zero).
This is all actually called by cfn-init in CloudFormation, but that is probably not relevant because we can reproduce it from a command line window.
The actual call is:
cmd.exe /C start /high /wait BatFileThatReturnsOne.bat
How do I get start to set the %ERRORLEVEL% to something other than 0 (zero)?
directly from a cmd window or a batch file you can use
start /high /wait cmd /c BatFileThatReturnsOne.bat
but if you need to start the cmd instance to execute the start command that launchs the batch file then you can use
cmd /v /e /c" start /high /wait cmd /c launched.cmd & exit ^!errorlevel^!"
Just change EXIT /B 1 by EXIT 1.
As explained in the Table 4 given in this answer about START /WAIT bat command:
When the started Batch file ends, set ERRORLEVEL = value from 'EXIT number' commmand.
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
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.
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