Why is command start with parameter /wait not working properly? - windows

OK this is racking my brain!!! I have one batch file that will start another batch file but every time I run say batch file all it does is open a command window with the title being where the batch file is located. Here is the batch file that's preforming the start /wait command:
::------------------------------ configure power settings ---------------------------
#echo off
start /wait "%~d0\SETUP_POSTOP\01 Configure Power Settings\always on.bat"
::------------------------------ programs and features ------------------------------
start /wait "%~d0\SETUP_POSTOP\02 Uninstall Unwanted Software\Programs and Features.bat"
The above batch file is supposed to run "always on" batch file but all it does is open another command window. Here is the "always on" batch file its trying to start:
#echo off
echo DO NOT CLOSE!!!
%windir%\system32\powercfg.exe /import "%~d0\SETUP_POSTOP\01 Configure Power Settings\alwayson.pow" 2f5ac084-2edf-444a-b1b9-8de872cf798e
%windir%\system32\powercfg.exe /setactive 2f5ac084-2edf-444a-b1b9-8de872cf798e
start /wait %windir%\System32\powercfg.cpl
exit
I've tried everything and all my research is pointing to a bug in the start command? I'm just up in arms with this one!

The start command will handle the first quoted argument as the title of the new window.
Try with
start /wait "" "%~d0\SETUP_POSTOP\01 Configure Power Settings\always on.bat"

start "" /wait "%~d0\SETUP_POSTOP\01 Configure Power Settings\always on.bat"
The first double-quoted start command line parameter is treated as a window title.
Syntax: START "title" [/D path] [options] "command" [parameters]
Always include a title this can be a simple string ("My Script") or
just a pair of empty quotes "".
According to the Microsoft documentation, the title is optional, but
depending on the other options chosen you can have problems if it is
omitted.

Related

How start a program via command line after the previous process terminated itself?

I run a program like notepad.exe via command line.
After the process terminated itself after completion of task of the started application, I need to run another program via command line, for example winword.exe.
So I would like a behavior like a scheduler, waiting in background up to completion of first process and then initiate start of another process.
Can I achieve this in Windows? And if yes, how?
Batch file solutions
For the example Notepad.exe and Winword.exe the solution is quite simple with following batch file:
#echo off
%SystemRoot%\Notepad.exe
start Winword.exe
Windows command interpreter cmd.exe first starts Windows Notepad and halts execution of the batch file until Notepad.exe terminated itself which means the user
pressed Alt+F4 on keyboard, or
clicked on X symbol on right side of title bar of Notepad window, or
clicked in menu File on menu item Exit, or
double clicked on application symbol on left side of title bar of Notepad window, or
clicked once on application symbol on left side of title bar of Notepad window and clicked next on last application context menu item Close.
Then internal command START of cmd.exe is used to start Microsoft Word in a separate process parallel to running command process. For that reason cmd.exe continues the batch file processing immediately after executing START while Microsoft Word is running parallel and exits because there is no more command line.
The application to start must be specified usually with full path if the directory containing the executable is not included in environment variable PATH, enclosed in double quotes if the path contains a space or one of these characters &()[]{}^=;!'+,`~. Please take a look on What is the reason for '...' is not recognized as an internal or external command, operable program or batch file? for details on how Windows command interpreter finds executables and scripts specified on command line or in a batch file without path (and without file extension). So best would be as second line something like:
start "" "%ProgramFiles(x86)%\Microsoft Office\Office14\Winword.exe"
This command line starts 32-bit Microsoft Word 2010 installed in standard installation directory on a computer running 64-bit Windows. The additional empty argument string "" is necessary because command START would interpret otherwise the full qualified name of application to start enclosed in double quotes as title for a new console window. So the command START would start a new command process with the title C:\Program Files (x86)\Microsoft Office\Office14\Winword.exe for the console window without specifying explicitly an empty title with "" as first argument.
Why is using just start Winword.exe working?
The directory containing Winword.exe is not included in environment variable PATH. But as long as Winword.exe is installed at all, this command line nevertheless results in starting Microsoft Word. The reason is correct registration of application Winword.exe in Windows registry during installation. For that reason the command START is capable to find out where Winword.exe is installed and execute it. For details on how this works see answer on Where is “START” searching for executables?
The three lines in batch file can be also optimized to a single line with multiple commands:
#%SystemRoot%\Notepad.exe & start Winword.exe
But this single command line can't be used directly in a command prompt window because of cmd.exe executes in this case Windows Notepad and Microsoft Word parallel.
Command line solutions
The command line solution for usage directly from within a command prompt window is:
start /wait Notepad.exe & start Winword.exe
This starts Windows Notepad in a separate process using command START with explicitly waiting for termination of Notepad.exe because of using additionally the START parameter /wait before one more START is executed to start Microsoft Word. There is no need of "" as empty title string here because of no argument string in this command line is enclosed in double quotes.
But this command line solution has one disadvantage: the command prompt window can't be used further as long as Windows Notepad is running.
So better would be starting from within current command prompt window a new command process with minimized window which first executes Windows Notepad, halts command line execution until Notepad terminates itself, then starts Microsoft Word and exits immediately after starting Winword.exe. This can be done with following command line:
start "Notepad & Winword" /min cmd.exe /C "start /wait Notepad.exe & start Winword.exe"
This command line results in starting cmd.exe as separate process, with a console window being minimized because of option /min of command START, with a console window title Notepad & Winword, which closes itself because of option /C of CMD, after first starting Notepad.exe and waiting for Notepad termination before starting Winword.exe not waiting for termination.
Well, the additional, minimized console window with title Notepad & Winword is in real of no usage for the user. Therefore better would be the command line:
start "" /B cmd.exe /C "start /wait Notepad.exe & start Winword.exe"
The additional command process is started in this case with no window (in background) because of usage of option /B of command START. An empty title is specified here as no console window is shown at all.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
cmd /?
echo /?
start /?

How to launch this program from the command line?

This is the program I am trying to launch from command line (Star Guard)
I opened a new command prompt process in this directory and am trying to start the Star Guard application with command line(so I can pass in command line arguments later).
However when I do so, I don't end up launching the program but I do end up launching a new command prompt process in the same directory.(output shown below) The program launches fine when I start it normally(GUI click)
Does anyone know what the issue is? I first did the ls(OSx) equivalent in Windows to make sure I had the right file path for the executable. I then used the start command to start the program along with enclosing the executable path in quotation marks to account of the spaces.
The syntax of the start command is unique, not to mention daft. If the first argument is in quotes it is interpreted as a window title.
In this case, you don't actually need to use start at all, you could just say
"Star Guard"
or
"Star Guard.exe"
If you want to use start, perhaps because you want to specify /wait for a GUI application or because you want to launch a console application in a new window, you have to include the title argument:
start "" "Star Guard.exe"
(The title argument doesn't need to actually specify a title, it just needs to be present.)
From OS/2 Warp Help
Starts an OS/2 program in another session.
The primary use for START is to automatically start programs at system startup. The
special batch file, STARTUP.CMD, allows you to do this.
To imbed redirectional signals into the command session, enclose the command and
command inputs in quotation marks.
START
"program /K /F
title" /C /B
/N
/PGM /FS /MAX
/WIN /MIN
/PM
/DOS
command
/I command
inputs
Related Commands: RUN
Enter this command without a parameter to start an OS/2 command processor.
If you use the /WIN, /FS, or /PM parameter, your program runs in the foreground session.
If you do not use one of these parameters, you can use the /F parameter to make the
program run in the foreground session.
Make sure that you specify the correct drive and path when you use the START command to
run a batch file with the STARTUP.CMD file. Also, if you plan to redirect I/O using the
START command, enclose the command and command inputs within quotation marks.
You can use START to run full-screen applications or applications running in a window
such as Presentation Manager programs.
START determines the type of application and will run it in the appropriate window or
full-screen session. However, you have the option to override the determined default by
using the /FS, /WIN, /PM, or /I parameter.
You cannot start a batch file (.CMD) with the /PM parameter.

How do I launch a program from command line without opening a new cmd window?

I'm trying to programmatically execute an external file from cmd using this command:
START "filepath"
Where "filepath" is the path of my file. It opens fine but it also open a new command prompt window.
So, which is the right command for opening an external program without opening a new window?
In Windows 7+ the first quotations will be the title to the cmd window to open the program:
start "title" "C:\path\program.exe"
Formatting your command like the above will temporarily open a cmd window that goes away as fast as it comes up so you really never see it. It also allows you to open more than one program without waiting for the first one to close first.
Add /B, as documented in the command-line help for start:
C:\>start /?
Starts a separate window to run a specified program or command.
START ["title"] [/D path] [/I] [/MIN] [/MAX] [/SEPARATE | /SHARED]
[/LOW | /NORMAL | /HIGH | /REALTIME | /ABOVENORMAL | /BELOWNORMAL]
[/NODE <NUMA node>] [/AFFINITY <hex affinity mask>] [/WAIT] [/B]
[command/program] [parameters]
"title" Title to display in window title bar.
path Starting directory.
B Start application without creating a new window. The
application has ^C handling ignored. Unless the application
enables ^C processing, ^Break is the only way to interrupt
the application.
Just remove the double quote, this works in Windows 7:
start C:\ProgramFiles\folderName\app.exe
If you want to maximize the window, try this:
start /MAX C:\ProgramFiles\folderName\app.exe
Your command START "filepath" will start a command prompt and change the command prompt title to filepath.
Try to run start /? in windows command prompt and you will get more info.
I think if you closed a program
taskkill /f /im "winamp.exe"
//....(winamp.exe is example)...
end, so
if you want to start a program that you can use
start "" /normal winamp.exe
(/norma,/max/min are that process value cpu)
ALSO
start "filepath"
if you want command line without openning an new window
you write that
start /b "filepath"
/B is Start application without creating a new window. The
application has ^C handling ignored. Unless the application
enables ^C processing, ^Break is the only way to interrupt
the application.
If you're doing it via CMD as you say, then you can just enter the command like so:
path\to\your.exe
which will open it within the same window. For example in C++:
system("path\\to\\your.exe"); // Double backslash for escaping
will open your.exe in the current CMD window. Likewise to start with a new window, just go for:
system("start path\\to\\your.exe");
If you go for the first option, you would have to clear your screen unless you wanted to have the command to open your.exe on the screen still.
You can use the call command...
Type: call /?
Usage: call [drive:][path]filename [batch-parameters]
For example call "Example File/Input File/My Program.bat" [This is also capable with calling files that have a .exe, .cmd, .txt, etc.
NOTE: THIS COMMAND DOES NOT ALWAYS WORK!!!
Not all computers are capable to run this command, but if it does work than it is very useful, and you won't have to open a brand new window...
I got it working from qkzhu but instead of using MAX change it to MIN and window will close super fast.
#echo off
cd "C:\Program Files (x86)\MySQL\MySQL Server 5.6\bin"
:: Title not needed:
start /MIN mysqld.exe
exit
20190907
OS: Win 10
I'm making an exe in c++, for some reason usting START make my program fail.
So, just use quotes:
"c:\folder\program.exe"
It's easier and safer to use the explorer.exe to start files/executeables, it is also opening files with the associated program and runs executables directly, if you execute:
start "" "%windir%\explorer.exe" "C:\Path\To\some.pdf"
If *.PDF files are not associated with any program. This works similar to a double-click and triggers the "open with program" dialog.
start "" "%windir%\explorer.exe" "%windir%\system32\calc.exe"
Will bring up the calculator.
start "" "%windir%\explorer.exe" "C:\pagefile.sys"
Will bring up a warning like: You are trying to open a system file (*.sys)... choose a programm...
Important
explorer.exe always requires the full path to the executable/file, not the relative path.
If you want to use relative paths, use
start "" "%windir%\explorer.exe" "relative\file.pdf","C:\dir\"
Security related
Using start "" "some\file.dat" will try to execute the file as binary file and start it, if possible. This can end in security related issues.
You can test it by creating a copy of cmd.exe calling it dummy.dat and start it using start dummy.dat
1-Open you folder that contains your application in the File Explorer.
2-Press SHIFT and Right Click in White Space.
3-Click on "Open command window here".
4-Run your application. (you can type some of the first characters of application name and press Up Arrow Key OR Down Arrow Key)

CMD Script: How to close the CMD

I have created a small command that will let me launch Internet Explorer. However, I wish to close the small command prompt that shows up when I launch IE. How can I do this? This is my current code:
"%ProgramFiles%\Internet
Explorer\iexplore.exe"
http://localhost/test.html
PAUSE
I am guessing if I take out the Pause. It will close the CMD box upon closing IE??
Also is there another command that I can use to simply create a command that will let me add something to the Menu with a small icon, which in turn runs the above. Is this complicated? Any tutorials I can use?
Thanks all
Use the start command:
start "title" "%ProgramFiles%\Internet Explorer\iexplore.exe" http://www.example.com
you need this on the end
&& exit
For example
"%ProgramFiles%\Internet Explorer\iexplore.exe" http://google.co.uk && exit
#echo off
start "" "%ProgramFiles%\Internet Explorer\iexplore.exe" "http://www.example.com"
exit /b
But you really should not force IE, but use the default browser:
#echo off
start http://www.example.com
exit /b
exit /b does not work on win9x IIRC, so if you need to support every version of windows and close the terminal window if the user double clicks your batch file, go with:
#echo off
start http://www.example.com
cls
You can also launch your program with the /c switch, which terminates the cmd once its finished executing
for example
cmd /c "%ProgramFiles%\InternetExplorer\iexplore.exe" http://localhost/test.html
You have to add 'start' in front of every program you launch, elsewhere your script is going to wait until it's finished.
A little late here, but running it in minimized mode or invisible mode might be another option. Source: https://www.winhelponline.com/blog/run-bat-files-invisibly-without-displaying-command-prompt/
Running .BAT or .CMD files in minimized mode
To run a batch file in a minimized window state, follow these steps:
Create a shortcut to the .BAT or .CMD file. To do so, right click on the file, click Send To, Desktop (create shortcut)
Right click on the shortcut and choose Properties
In the Run: drop down, choose Minimized
Click OK
Double-click the shortcut to run the batch file in a minimized window state.
"Mind the gap!"
Command Prompt always takes the empty space as separator, unless it's enclosed in double quotes.
So, if any Path, or Program/File Name, or anything includes empty space/es, must closed in quotes.
eg. "C:/Program files/..." path/directory or "Any Program/Command/File.exe/cmd/txt..." Program/Command/File Name includes space/es.
Syntax:
> start /?
Starts a separate window to run a specified program or command.
START ["title"] [/D path] (start swiches here...) [command/program] (com/prog-parameters here)
start "" /d "Drive:/the/Program/Path/..." "Command/Program Name.extension" "File-Name.extension"
So, it's usual fault:
If you don't set the 1st set of quotes "" for title (even if there's nothing to enclose), then the START command takes whats inside the 1st quotes set (eg. path! or Program Name!) and sets it as title... and of course, it messing up.

How to prevent auto-closing of console after the execution of batch file

What command can I put at the end of a batch file to prevent auto-closing of the console after the execution of the file?
In Windows/DOS batch files:
pause
This prints a nice "Press any key to continue . . . " message
Or, if you don't want the "Press any key to continue . . ." message, do this instead:
pause >nul
Depends on the exact question!
Normally pause does the job within a .bat file.
If you want cmd.exe not to close to be able to remain typing, use cmd /k command at the end of the file.
If you want cmd.exe to not close, and able to continue to type, use cmd /k
Just felt the need to clarify what /k does (from windows website):
/k : Carries out the command specified by string and continues.
So cmd /k without follow up command at the end of bat file will just keep cmd.exe window open for further use.
On the other hand pause at the end of a batch file will simply pause the process and terminate cmd.exe on first button press
If you are using Maven and you want to skip the typing and prevent the console from close to see the result you need to use the CALL command in the script, besides just the 'mvn clean install'.
Like this will close the console
ECHO This is the wrong exemple
mvn clean install
pause
Like this the console will stay open
ECHO This is the right exemple
CALL mvn clean install
pause
If you dont use the CALL command neither of the pasts exemples will work. Because for some reason the default behaviour of cmd when calling another batch file (which mvn is in this case) is to essentially replace the current process with it, unlike calling an .exe
The below way of having commands in a batch file will open new command prompt windows and the new windows will not exit automatically.
start "title" call abcd.exe param1 param2
start "title" call xyz.exe param1 param2
Add cmd.exe as a new line below the code you want to execute:
c:\Python27\python D:\code\simple_http_server.py
cmd.exe
my way is to write an actual batch (saying "foo.bat") to finish the job; then create another "start.bat":
#echo off
cmd /k foo.bat
I find this is extremely useful when I set up one-time environment variables.
Call cmd at the end of the batch file.
Had problems with the answers here, so I came up with this, which works for me (TM):
cmd /c node_modules\.bin\tsc
cmd /c node rollup_build.js
pause
besides pause.
set /p=
can be used .It will expect user input and will release the flow when enter is pressed.
or
runas /user:# "" >nul 2>&1
which will do the same except nothing from the user input will be displayed nor will remain in the command history.
This little hack asks the user to enter a key and stores it into the variable %exitkey% (this variable could be called anything you like though).
set /p exitkey= "Press any key to continue..."
NB: the space after the '=' is very important
I know I'm late but my preferred way is:
:programend
pause>nul
GOTO programend
In this way the user cannot exit using enter.
Possibility 1:
Just make 2 .bat files and write into the first:
start <filename> // name of 2nd batch file
exit
Batch file 2 is the file that wont close in the end.
So now when you open batch nr.1 It will start the 2nd and cloe itself.
When the 2nd finishes it will not close entirely (as long as you wont put exit at the end)
Possibility 2:
Batch file 1:
call <filename>
cls
echo End of file
pause
<any code you want>
When the 2nd file ends then it will proceed to file 1 again and output the rest of it. With that you can even make error handlers. If nr.1 crashes it goes into nr.2 and displays it
There are two ways to do it depend on use case
1) If you want Windows cmd prompt to stay open so that you can see execution result and close it afterwards; use
pause
2) if you want Windows cmd prompt to stay open and allow you to execute some command afterwords; use
cmd
pause
or
echo text to display
pause>nul
Easy, add cmd to your last line of bat, BUT! if you reset or clear your system path, you must start your cmd with the full path, like:
%windir%\system32\cmd.exe
For example, I have a bat file to reset jdk to old version like this:
PATH=C:\Program Files\Java\jdk1.6.0_45\bin;C:\apache-ant-1.7.1\bin
SET JAVA_HOME=C:\Program Files\Java\jdk1.6.0_45
%windir%\system32\cmd.exe
since I reset the system path, I have to run cmd with the full path, or the system can't find cmd.exe, it will fail to run cmd, and just close the window, and you can't see the error msg.
Run the .exe file and then pause the cmd
batch script example :
#echo off
myProgram.exe
PAUSE
batch script example with arguments :
#echo off
myProgram.exe argumentExample1 argumentExample2
PAUSE
I added #echo off because I don't want to show C:\user\Desktop>myProgram.exe and C:\user\Desktop>PAUSE in the cmd
cmd /k cd C:\Projects.....
If you want your cmd opened at specific long location
add pause (if you don't want anything else to show up add) >nul it should look like:#echo offtitle niceecho hellopause >nulall you will see is "hello"
I personally put pause >nul and it waits for a key to be pressed without showing any extra text in the console.
using : call yourbatch.cmd
does the job
will process the script and then continue ejecuting other code on same window (cmd instance)

Resources