1.

Solve : Batch File Help D:?

Answer»

I need to write a batch file that CHECKS to see if a process is running, and then either kill it or close itself depending on if it is running.

Not just once though, say I have 10 Firefox's up, I would want it to close one, and then check, close, check, etc. until they are all closed, and then close itself.

Even though I don't know the actual commands and how the errorlevels read, I would think it would look something like this:

@echo off
:START
check firefox.exe
if ERRORLEVEL ==2 goto END
if errorlevel ==1 goto KILL
:KILL
taskkill /f /im firefox.exe
goto START
:END

Any suggestions are appreciated!taskill /f /im [imagename] kills all instances of imagename. So you would only need to run it once.

Code: [Select]@echo off
REM start 10 notepads
echo Starting 10 notepads...
for /L %%N in (1,1,10) do start /MIN "" "notepad"

echo Press a key to stop them all...
pause>nul

REM kill them all
taskkill /f /im "notepad.exe"


Code: [Select]Starting 10 notepads...
Press a key to stop them all...
SUCCESS: The process "notepad.exe" with PID 2208 has been terminated.
SUCCESS: The process "notepad.exe" with PID 1308 has been terminated.
SUCCESS: The process "notepad.exe" with PID 3480 has been terminated.
SUCCESS: The process "notepad.exe" with PID 1548 has been terminated.
SUCCESS: The process "notepad.exe" with PID 2644 has been terminated.
SUCCESS: The process "notepad.exe" with PID 3928 has been terminated.
SUCCESS: The process "notepad.exe" with PID 1200 has been terminated.
SUCCESS: The process "notepad.exe" with PID 3624 has been terminated.
SUCCESS: The process "notepad.exe" with PID 3528 has been terminated.
SUCCESS: The process "notepad.exe" with PID 1340 has been terminated.:O! You're right, I forgot it did it for all instances, but still.. say I wanted it to check to see if an .exe is running, and then open it if it isn't? Kinda the opposite, but I kinda wanna know how it works both ways. Code: [Select]tasklist /fi "imagename eq notepad.exe" /fi "status eq running" | find "No tasks are running which match the specified CRITERIA">nul
if %errorlevel% equ 0 start notepad.exe
Gunna go give it a whirl!

Edit: It opened another even if the first is still running. D:Quote from: JooseyJay on July 04, 2010, 05:58:30 AM

Gunna go give it a whirl!

Edit: It opened another even if the first is still running. D:

It doesn't on my computer. If you run it and notepad is already running it does nothing, just exits.

I have vista 64-bit, and it checks if notepad is running, but whether it is or not, it says "there are no tasks running that meet the criteria." or similar.Are you sure you copied it CORRECTLY? Are you usng the CORRECT image name?


Discussion

No Comment Found