I need help writing batch code.
In the initial state of my batch script I need to check if notepad.exe
is running
if it is running then taskkill /im notepad.exe
elsif notepad.exe
is not running then go to next batch statement/code.
For Example - To kill Notepad, run the command as, taskkill /IM notepad.exe /F, where /F is used to kill the process forcefully.
We can easily check whether a process is currently running or not using the tasklist toolkit. Tasklist allows us to check for current processes.
You can use the ps command to find out which processes are running and display information about those processes. The ps command has several flags that enable you to specify which processes to list and what information to display about each process.
taskkill /F /IM notepad.exe this is the best way to kill the task from task manager. Save this answer. Show activity on this post.
Try taskkill /fi "IMAGENAME eq notepad.exe"
Not finding notepad.exe will only throw an info instead of an error.
You can simply execute taskkill /im notepad.exe
in all cases. If it's not running, then taskill
will having nothing to kill and will just return.
In that situation, taskkill
will report an error and set the error level. You can suppress the reporting of the error by redirecting standard error:
taskkill /im notepad.exe 2> nul
As for the error level, you can just ignore that and it will be cleared by the next command that you execute. Or if needed, you can clear it yourself.
This approach is, in my view, better than trying to anticipate whether or not taskkill
will succeed. You won't be able to anticipate all possible failure modes and since taskkill
itself performs the very check that you are asking about, I think you may as well leave that check to taskkill
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With