- close a windows bat and do some actions
- 4 Answers 4
- How to close window with .bat script?
- 2 Answers 2
- Close programs from the command line (Windows)
- 5 Answers 5
- Update to Updated Question
- How to prevent auto-closing of console after the execution of batch file
- 17 Answers 17
- How to automatically close cmd window after batch file execution?
- 10 Answers 10
- Not the answer you’re looking for? Browse other questions tagged batch-file cmd or ask your own question.
- Linked
- Related
- Hot Network Questions
- Subscribe to RSS
close a windows bat and do some actions
here my question is I want to close a windows bat dos window directly, and when the close action is invoked, some actions or another bat should be invoked, how can i implement this?
what I am doing is like this : I start a bat, and the bat start two processes, when the dos bat closed,the two process should also be closed. The users often close the dos window directly,don’t use the stop.bat I provided, So I am thinking about is it possible to catch the user’s close action and do something!!
4 Answers 4
Well, I have solved this problem. the main idea is from [Windows shutdown hook on java application run from a bat script], but the answer has a bug, that is if I close the Bat or Cmd forcibly,The action may not be invoked, the reason here is The JVM shutdown before the java code action invoked. so I put the action in native method.
I recently did something like this. Basically the way I managed to get it work was that I had another batch file (we’ll call it bat2) launch when we started the main batch file (we’ll call this one bat1).
That’s the script I used for bat2. It’s probably not as good as it could be but I’m very new to this stuff so it’s the best I could come up with.
Hope this helps you!
Just a note on the previous entry by AstralBacon. You can simplify it like so:
Using a second Batch is the way to go as the other answers have suggested.
But searching through your entire verbose tasklist with «tasklist /v» is a very slow and ineffective process. Takes multiple seconds on my machine. You can manipuate the tasklist command output to only show your batch (which is way more effective) and use that output directly like this without needing a pipe:
Explanation: First we fetch a single line of csv-style tasklist output which would either look like this if it finds your batch:
or like this if there’s no such window title in the tasklist:
We throw this into that FOR /F loop that runs only once and only picks the first token:
«cmd.exe» or «INFORMATION: blablabla. «.
Now «cmd.exe»/»INFO. » is in variable %G that can be used in an IF statement to get the definitive answer wether the other task is alive or dead.
Bonus fact: You can use a single .bat file to launch both scripts simultanously like this:
How to close window with .bat script?
I have ran into a slight problem while configuring some keybinds with my mouse.
The thing I want to achieve is to open the program from the backround(open or closed window) and close it again, but not kill the task(eqivalent to the x-button, so it keeps running in the backround)
I have set up my mouse to start up a application called everything (which is able of very fast file system searches)
I have figured out that with this code:
which I can run with a macro key I can kill the application. But I dont want to kill the application itself just the window, becase it takes a while to index all the important files once I have to restart it.
So heres the question is there any way to bring up a window/task to the front of the screen or trigger the x button event, without having to kill the entire process ?
Also the Mouse(g600) supports lua scripting, but that is proabbly the more harder way to go about it.
2 Answers 2
Minimizing a window can be usually done by activating the window (bringing it to the foreground) then using SendKeys() to send Alt + Space , then whatever letter your locale has underlined for «Minimize» ( N in English Windows). Here’s one of possibly dozens of examples of this using VBScript.
That sequence is overused, inelegant, and boring in my opinion — and it doesn’t work for some windows (the cmd console, for example). It is possible to minimize a window without bringing it to the foreground, and without simulating key presses. One simply needs to import a function from user32.dll. And as long as we’re going through that trouble, we might as well import a second function to detect the current window state so we can toggle minimized / restored.
We can import the functions using PowerShell like this. Save this Batch / PowerShell hybrid script with a .bat extension and give it a run.
Now, if your program minimizes to the systray, the previous script will be fine for minimizing, but won’t be able to find the window handle when minimized. It will fail when trying to restore. Finding the HWND in this situation is tricky. If one knows the window title, one can find the HWND using the user32.dll FindWindow() or FindWindowEx() functions.
I haven’t had much luck using WMI or built-in PowerShell commands for finding the title of a window minimized to the tray. Neither get-process nor gwmi win32_process nor [diagnostics.process]::getProcessByName() yielded success. I did, however, discover that the final line of tasklist /v /fo list will show the window title. That’s enough to invoke FindWindow() and get the HWND.
This might be more complicated than it needs to be, but then again it might not. I just know I found 1,000 ways to fail, but one way to succeed. Thanks to JPBlanc for helping me figure out how to pass null arguments into FindWindow() and FindWindowEx() .
Close programs from the command line (Windows)
What is the proper way to close/exit programs from command line, similar to pressing the «X» close button in the corner of the window?
Im trying to close chrome under 3 different versions of windows: win7 ultimate, win7 home, winXP
Under Ultimate and XP: TSKILL chrome
Under Home: TASKKILL /IM chrome.exe
(It closes chrome, no cmd errors,
but chrome error restore when relaunch it)
TASKKILL /IM chrome.exe :
(It closes chrome, no chrome errors when relaunch it,
but errors in cmd: «impossible to terminate child processes(about 4-5), only by force with /F «)
Should I ignore cmd child errors if on relaunch chrome show me no errors?
5 Answers 5
The proper way to close/exit a program ultimately depends upon the software. However, generally the best practice is for Windows programs to close whenever they receive the WM_CLOSE message. Properly releasing memory and closing handles. There are other messages that can signal the close of the application, but it is up to the author of the software how each message is handled.
taskkill sends the WM_CLOSE message and it is then up to the application whether to properly close. You may also want to use the /T option to also signal child processes.
Only use the /F option if you want to force the termination of the process.
Other options would include sending the Alt+F4 keys, using PowerShell, or 3rd party applications.
Update to Updated Question
Ignore, the errors. Chrome generates many processes. The errors are caused when an process does not acknowledge the WM_CLOSE message that TASKKILL sends. Only processes with a message loop will be able to receive the message, therefore, the processes that do not have a message loop will generate that error. Most likely, these processes are the chrome extensions and plugins.
To hide the errors capture the output
Summary: TASKKILL is the proper way via command line to close applications per its WM_CLOSE implementation and the Microsoft KB that I linked.
It has «closeprocess» command which is designed to close processes gracefully. As per its document, it does not terminate apps but sends WM_CLOSE to all top-level windows of the target process.
If this doesn’t work, I bet your application has an unusual cleanup procedure. I would like to see what happens inside so please let me know what your target application is.
The answer to that question can be found here (Microsoft link).
You can send WM_CLOSE messages to any window you wish to close. Many windows handle WM_CLOSE to prompt the user to save documents.
A tool that does this correctly is @Kill . Look also SendMsg.
I do not know how to do this in batch, but you could use the vbscript for this. Simulating the Alt + F4 keys (equates to signal WM_CLOSE ).
Run and look at the behavior of this script below.
Here is the list key names for SendKeys.
When you run the script, the notepad is open, some words are written and then a signal to close the program is delivered, see picture below.
Additional Questions
Can I start a program minimized, or background with vbscript?
Yes. Use the following code:
For more information check the Run Method .
Can chrome go to some url in vbscript?
If chrome is the default, use:
Is there a way to bring focus to specific application (chrome.exe)?
I want to send alt+f4 ONLY to chrome, independently of i’m doing with other windows.
The following code works on Windows 8.
There are some command-line utilities that can send a suitable WM_SYSCOMMAND message (with SC_CLOSE as the command) to a program’s top-level window. I’m sure that at least one will be mentioned shortly. (Then someone will mention AutoIt. Then there’ll be an answer showing how to do it with PowerShell and CloseMainWindow() .)
The command-line utility that comes as a built-in command in JP Software’s TCC, a command interpreter and command script processor for Windows, is called TASKEND .
Alright, Not going to lie. I saw this on stackoverflow and thought it was a challenging question. Soooo I just spent the last 2 hours writing some code. And here it is.
After following the steps below, you can type «TaskClose notepad.exe» after hitting «Start» and it will auto save all undocumented notepad files into desktop. It will auto-close chrome.exe and save the restoration settings.
You can add and remove additional settings for other applications under the if conditions. For instance:
The vbs and batch files performs the following procedures:
- Collects the executable.
- Queries the executable application names off of the tasklist.
- Performs an «Alt+TAB(x)» procedure until it has verified the window is open.
- Then Executes the rolling commands whether it be «Alt+F4» or even in extreme cases
- Alt+F4
- Activate Save
- AutoIncrememnt Filename
- Exit application.
ReturnAppList.bat : install in «C:\windows\system32\»
TaskClose.bat : install in «C:\windows\system32\» AND «C:\Users\YourUserName\»
TaskClose.vbs : install in «C:\windows\system32\»
This was alot of fun to write and I’m more happy about finishing it than actually showing the answer. Have a great week!
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?
17 Answers 17
In Windows/DOS batch files:
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:
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
Like this the console will stay open
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
How to automatically close cmd window after batch file execution?
I’m running a batch file that has these two lines:
This batch file is used to run the Xming application and then the PuTTY app so I can SSH into my university’s computer lab.
However, if I run this and Xming is not already open, once I exit from the PuTTY terminal the cmd window remains open. Only if I have already run Xming does the cmd window close when I close the PuTTY terminal. I’ve tried adding exit to the last line of the batch file, but to no avail.
10 Answers 10
Modify the batch file to START both programs, instead of START ing one and CALL ing another
If you run it like this, no CMD window will stay open after starting the program.
You normally end a batch file with a line that just says exit . If you want to make sure the file has run and the DOS window closes after 2 seconds, you can add the lines:
But the exit command will not work if your batch file opens another window, because while ever the second window is open the old DOS window will also be displayed.
SOLUTION: For example there’s a great little free program called BgInfo which will display all the info about your computer. Assuming it’s in a directory called C:\BgInfo , to run it from a batch file with the /popup switch and to close the DOS window while it still runs, use:
If you want to separate the commands into one command per file, you can do
and in the other file, you can do
The command cmd /c will close the command-prompt window after the exe was run.
This worked for me. I just wanted to close the command window automatically after exiting the game. I just double click on the .bat file on my desktop. No shortcuts.
To close the current cmd windows immediately, just add as the last command/line:
Try move nul to nowhere and redirect the stderr to stdin will result in the current window cmd.exe being closed
This is different from closing a bat, or exiting it using goto :EOF or Exit /b
I even opened a question to find an answer that would explain behavior. Would that be a bug? Immediate closing of the current cmd window when executing: move nul 2>&0
I had this, I added EXIT and initially it didn’t work, I guess per requiring the called program exiting advice mentioned in another response here, however it now works without further ado — not sure what’s caused this, but the point to note is that I’m calling a data file .html rather than the program that handles it browser.exe , I did not edit anything else but suffice it to say it’s much neater just using a bat file to access the main access pages of those web documents and only having title.bat , contents.bat , index.bat in the root folder with the rest of the content in a subfolder.
i.e.: contents.bat reads
It also looks better if I change the bat file icons for just those items to suit the context they are in too, but that’s another matter, hiding the bat files in the subfolder and creating custom icon shortcuts to them in the root folder with the images called for the customisation also hidden.
You could try the somewhat dangerous: taskkill /IM cmd.exe ..dangerous bcz that will kill the cmd that is open and any cmd’s opened before it.
Or add a verification to confirm that you had the right cmd.exe and then kill it via PID, such as this:
Substitute (pslist &&A) or (tasklist /FI «PID eq %%A») for the (taskkill /PID %%A) if you want to check it first (and maybe have pstools installed).
Just try /s as listed below.
As the last line in the batch file type:
The above command will close the Windows CMD window.
/s — stands for silent as in (it would wait for an input from the keyboard).
This works for me
Sometimes you can reference a Windows «shortcut» file to launch an application instead of using a «.bat» file, and it won’t have the residual prompt problem. But it’s not as flexible as bat files.
Not the answer you’re looking for? Browse other questions tagged batch-file cmd or ask your own question.
Linked
Related
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.4.16.39093
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.