I am programming a simple program that will close a process the the user inputs.
For example i will write Chrome , explorer , and then the program will close the chosen process.
My only problem is how do i actually close the chosen process aka p.
Code:
#echo off
#echo Thank you for using my program.
set /p id=Enter Process Name With .exe in the end:
taskkill p /im explorer.exe
timeout 5
%0
You may like to use this line:
taskkill /p /im %id%
or this one which forces the process closed, but it may corrupt data depending on which program is being forced closed.
taskkill /p /f /im %id%
Related
I'm trying to make a batch file that opens a certain exe and then, after (for example) 5 minutes, closes it then reopens it again. I have tried this:
#echo off
:loop
cd /d %~dp0
certain exe
timeout /t "time" (by minutes)
taskkill /f /im "certain exe"
goto loop
but it wouldn't close the exe nor open it, what can I do?
The problem is that cmd will wait for the current command to end before executing the next command, so timeout (and taskill) will not be executed until example.exe closes by itself.
To ensure that cmd doesn't wait example.exe, you'll need to use start /B, as in this example:
#echo off
:loop
start /B "example.exe"
ping -n seconds_of_delay+1 127.0.0.1>nul
taskkill /f /im "example.exe"
goto :loop
Replace seconds_of_delay+1 with the number of seconds you want the time period to last plus one.
/B is needed because, without it, start would execute example.exe in a new window (if you want that, simply use start).
I used ping as the way to create a time delay because it has been found to consume less processor time than sleep or timeout (you can find more details here).
I have some software for Windows that opens a website on exit. I want to run a batch that closes the browser after exit from example.exe
example.exe
(wait for end of example.exe)
taskkill /im firefox.exe
taskkill /im iexplore.exe
taskkill /im chrome.exe
The problem you are observing is probably that at the end of your game, the browser is started as another process. It can then take a while before the browser actually starts but the game doesn't wait and just exits. So when you come at the taskkill commands, the browser is still not open. What you need is a way to verify if a browser-process already started, not a way to find out if the exemple.exe (or your game) exited or not. You can do it this way:
:waitOnBrowser
tasklist | findstr /B /C:"chrome.exe" /C:"firefox.exe" /C:"iexplore.exe" > nul
REM the errorlevel will only be 0 if one of the browser has been found in the tasklist
IF ERRORLEVEL 1 goto :waitOnBrowser
REM At this point, you're sure at least one browser has been started
REM taskkill can come here
IF ERRORLEVEL 1 is kind of the same as saying IF %ERRORLEVEL% GTR 1 (see IF in batch).
On Microsoft Technet I can read that taskkill has a /f parameter to kill a process forcefully. I wonder what this does internally, to understand the impact of such an action.
taskkill (without /f) does not simply send a WM_CLOSE message to the process, otherwise my application would ask whether or not to save the open documents. This makes me assume that it already operates on a TerminateProcess (MSDN) level. However, TerminateProcess does not have a parameter for forcing a kill.
So, what do taskkill and taskkill /f do internally?
I read the related question Difference between C# Process.Kill() and Taskkill but it does not have an answer.
Most likely taskkill /f uses TerminateProcess, where as taskkill without /f just posts a WM_QUIT message (not WM_CLOSE). The docs says that TerminateProcess unconditionally kills the process.
You can try following experiments:
Launch notepad.exe and type a few chars in the notpad window
Type taskkill /f /im notepad.exe. Notepad will quit immediately
Now do this:
Launch notepad.exe and type a few chars in the notpad window
Type taskkill /im notepad.exe. Notepad won't quit immediately but it will quit ask if you want to save modifiactions.
There is this application build process that I am trying to automate. For this i wrote a java file, which runs every 24 hours.
A batch file is called from here that runs the application build whenever it is called.
I've run into a small problem, when the build fails due to incomplete or invalid files, a window pops up which tells me to look at the logs.
Since I haven't written the build files, I'm not really sure where this gets created from. I wanted to know if I can close this window while the process runs from the bat file.
It may be possible using taskkill, but you'd have to devise a filter that would ideally only match the process displaying the window and never match any other process. Something like:
taskkill /im program.exe
or maybe:
taskkill /fi "windowtitle eq title*"
You might also want to include the /f flag for forceful termination.
You'd also have to try and make sure that the taskkill command doesn't run too quickly and precede the creation of the popup window. You could try to query for the existence of such a process/window; your best bet here is probably wmic. Maybe:
#echo off
setlocal enabledelayedexpansion
set title=Notepad
set pid=
for /f %%i in ('wmic process where "caption like \"%%!title!%%\"" get processid^| findstr /r [0-9]') do #set pid=%%i
if "!pid!" neq "" taskkill /f /pid !pid!
There's no guarantee this will always work, but it's probably the best you can do.
Is it possible to check how many of a program are running via a batch file?
I made a program to use for tab for a cause and this is the code:
:1
timeout /t 2
start chrome.exe
start chrome.exe
start chrome.exe
start chrome.exe
timeout /t 7
taskkill /f /im chrome.exe
goto :1
But this program sometimes open more than 4 copies of chrome before closing them, up to 8. Is there a command that I could use which will say how many of the program are open? kinda like the below:
#echo off
:1
start chrome.exe
if "4 of chrome.exe are open" goto :2 else goto :1
:2
timeout /t 7
taskkill /f /im chrome.exe
timeout /t 2
goto :1
I kept on getting errors saying that it is not formatted properly until I put in all the spaces where the code is, why do you have to do that?
It's difficult to treat chrome's processes because it normally creates a separated process for each plug-in (add-on). But answering your question:
Is there a command that I could use which will say how many of the program are open?
Yes, you can see how many processes with same name are open at the moment:
tasklist|find /c "chrome.exe"
For using this within an if statement you can do the following:
for /f %%a in ('tasklist^|find /c "chrome.exe"') do (set chromeProcesses=%%a)
if "%chromeProcesses%" == "4" (do something...)