CMD Script: How to close the CMD - windows

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.

Related

Run appcmd command from shortcut

Very often I have to make following steps in command line (to identify correct worker process in IIS for debugging):
Run cmd as an administrator
cd %systemroot%\system32\inetsrv
appcmd list wp
I want to make a shortcut on the desktop to do it in one click.
How can I achieve that?
P.S. I tried to specify it like on the picture but it doesn't work, just opens cmd in inetsrv folder
I assume you set up the shortcut to run as admin, so I do not go into details on that.
What is missing in your Target line is the switch to tell cmd.exe that there are strings following which should be interpreted and executed as command. You can chose /C for execute and quit (cmd window disappears) or /K for execute and show prompt (cmd window stays open). So the result would be something like: "%windir%\system32\cmd.exe" /K "appcmd list wp". See cmd.exe /? for details.
GL, HF :)

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

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.

cmd.exe doesn't terminate when using a .bat file

[Context: I'm trying to create a shortcut to a .bat file with a relative "Start in" path as roughly described here and here.]
cmd.exe supports the /c switch. According to the documentation, this should cause it to "carry out the command and then terminate."
But the switch seems to be ignored when the command is a .bat file.
For example, if you create a shortcut with the following Target (to a normal, non-bat command):
C:\Windows\System32\cmd.exe /c "START /d C:\temp\ notepad.exe test.txt"
Everything works as expected: Notepad opens and the console (shell) disappears. But if you replace the command above with a .bat file instead, like so:
C:\Windows\System32\cmd.exe /c "START /d C:\temp\ C:\test.bat"
(where test.bat contains only "notepad.exe test.txt") Notepad opens as before but the console sticks around like an unwanted friend. Why? And more to the point, How do I make it go away?
UPDATE: I know I can use wscript, as in this solution, but then I lose the option of having a custom icon (I'm stuck with the default .vbs icon).
The start command begins a new process for the batch file. The original cmd.exe then terminates, but leaves the new process, which hangs around because it's waiting for notepad.exe to terminate.
Change your bat file contents to:
start "" notepad.exe test.txt
Then your batch file will not wait for notepad to exit before continuing execution.
Another thing to try:
C:\Windows\System32\cmd.exe /c "START /d C:\temp\ C:\test.bat & exit"
The nuclear option would be to write a small program in the (compiled) language of your choice that launches the .bat file and then exits. Then you can give it a custom icon, and make it do whatever you like.
You might also take a look at Autoit from http://autoitscript.com as an alternative to batch. - the Run() command can do this kind of thing with better predictability. Since it makes an executable you can link this from a shortcut directly. You can also do a whole lot more of course, like run as a different user, insert delays or handle errors, that are hard to do with batch.
You don't need the full kit, just the Aut2EXE folder from the download will do.
BTW, build your exes without UPX compression as that leads to AV false positives.
I'm a little late but here is the answer.
The documentation for start states:
Syntax
START "title" [/D path] [options] "command" [parameters]
If command 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 start is used to execute a batch file, the opened cmd instance wont close.
You could also use call instead.
call C:\test.bat

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)

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