How to find processes with identical name currently running, using a batchfile. And if there are more than one process with same name like myprocess.exe than it should kill it all. If there is only one process myperocess.exe than it should leave it.
list them, count them and if the counter is greater than one, kill them:
for /f %a in ('tasklist /nh /fi "imagename eq notepad.exe" /fo csv ^|find /c /v ""') do if %a gtr 1 taskkill /fi "imagename eq notepad.exe"
(this is command line syntax. To use it in a batchfile, replace every %a with %%a)
Related
I have been using the following script to check if a particular named window is open.
I got it from this thread:-
How do you test if a window (by title) is already open from the command prompt?
ideally I will expand the else part to close the window if it is found to be open.
#For /f "Delims=:" %A in ('tasklist /v /fi "WINDOWTITLE eq test.bat - Notepad"') do #if %A==INFO (echo Prog not running) else SET "BREX=Awesome" &echo %BREX%
Unfortunately when I run this script it returns three instances of my else string?
Is there any way to reduce this down to returning one instance?
You could use findstr instead. You're getting multiple lines of output as you're looping over each line of output
tasklist /v /fi "WINDOWTITLE eq test.bat - Notepad" | findstr /C:"No tasks are running"
if %errorlevel% NEQ 0 (
echo awesome
) else (
echo Prog not running
)
If you really want to do this with one line from the cmd prompt you can do this.
cmd /v:on /c "#For /f "Delims=:" %A in ('tasklist /v /nh /fi "WINDOWTITLE eq test.bat - Notepad"') do #if %A==INFO (echo Prog not running) else (SET "BREX=Awesome") &echo !BREX!"
Or use some conditional execution.
tasklist /v /nh /fi "WINDOWTITLE eq test.bat - Notepad" |findstr /B /C:"INFO: No tasks are running">nul && (echo Program not running) || (echo Awesome)
I'm trying to log the memory usage of browsers for exemple for Chrome / Firefox etc.
For Firefox I can simply use this little command line:
tasklist /fo csv /fi "imagename eq firefox.exe" > DumpResults.csv
And this will nicely result with one proces and its usage. But when applying this train of thoughts to Chrome you'll get around 4 processes even when you did a clean launch of Chrome. Is there any way to sum the results?
Sorry for the stupid question but it's my first attempt to create a bat file.
An alternative…
#Echo Off
Set "sum=0"
For /F "Tokens=6-7 Delims=., " %%a In (
'TaskList /NH /FI "ImageName Eq chrome.exe"') Do Set/A sum+=%%a%%b
Set sum
Pause
…prevents using two loops
#echo off
set sum=0
for /f "tokens=5 delims=," %%x in ('tasklist /fo csv /nh /fi "imagename eq chrome.exe"') do (
for /f "tokens=1-4 delims=.K " %%a in (%%x) do set /a sum+=%%a%%b%%c%%d
)
echo Sum Chrome = %sum%
Maybe you have to adapt the delimiters to your local settings. Output on my computer is like:
C:\Users> tasklist /fo csv /nh /fi "imagename eq chrome.exe"
"chrome.exe","7744","Console","1","86.388 K"
"chrome.exe","7784","Console","1","1.312 K"
"chrome.exe","7920","Console","1","50.188 K"
...
I want to write a simple batch file to kill a process that contains certain text in the window title. Right now I have:
taskkill /fi "Windowtitle eq XXXX*" /im cmd.exe
And that works, except what I want to do is use the wildcard both at the beginning AND end of the title. So something like:
taskkill /fi "Windowtitle eq \*X*" /im cmd.exe
But I tried this and it does not work. Is there something I'm missing or is this not possible?
No, wildcards are not allowed at the start of the filter.
for /f "tokens=2 delims=," %%a in ('
tasklist /fi "imagename eq cmd.exe" /v /fo:csv /nh
^| findstr /r /c:".*X[^,]*$"
') do taskkill /pid %%a
This will retrieve the list of tasks, in csv and verbose format (that will include window title as the last field in the output).
The list is filtered by findstr with a regular expression that will search the indicated text (the X) in the last field.
If any line matches the filter, the for will tokenize it, retrieving the second field (the PID) that will be used in taskkill to end the process.
In the special case you have started the command window from a batch file yourself, you can specify the window title using the command
START MyWindowTitle c:/MyProcess.exe
That way it is easy to kill the process again using just
taskkill /fi "WindowTitle eq MyWindowTitle"
A little more elaborate, but you can use:
for /f "tokens=2 delims== " %%A in ('tasklist /nh /fi "imagename eq cmd.exe" /fi "windowtitle eq MyWindowTi*"') do set "PID=%%A"
taskkill /F /T /PID !PID!
I get the number of a specific process with the help of this thread:
How to count amount of processes with identical name currently running, using a batchfile
I hope to assign the result of this command to a variable, then compare the variable with a number. My code is listed as below:
#echo off
setlocal enabledelayedexpansion
set procName=chrome.exe
set a=tasklist /FI "IMAGENAME eq %procName%" 2>NUL | find /I /C "%procName%"
if !a! equ 1 (
echo !a!
echo %procName% starts to run...
) else (
echo !a!
echo %procName% has not run!
)
Here I got '0' for
'set a=tasklist /FI "IMAGENAME eq %procName%" 2>NUL | find /I /C "%procName%"' command.
It also gives me "Echo closed" hint for 'echo !a!'.
FYI, when running the following command in cmd
tasklist /FI "IMAGENAME eq chrome.exe" 2>NUL | find
/c /i "chrome.exe"
set a=tasklist /FI "IMAGENAME eq chrome.exe" 2>NUL
| find /c /i "chrome.exe"
the output is 16 and 0 respectively.
What's the reason? How could I assign the result of a command to a variable? How to compare the variable to a number?
Thank you so much!
Well, set a=tasklist /FI "IMAGENAME eq chrome.exe" 2>nul | find /c "chrome.exe" does not work for me either. Which is good because I don't know how that was supposed to work.
I believe that this will be faster, because it doesn't have the overhead of FIND.EXE and writing, reading and deleting proc_temp.
set a=0
for /f "skip=3" %%x in ('tasklist /FI "IMAGENAME eq chrome.exe"') do set /a a=a+1
echo Total chrome.exe tasks running: %a%
EDIT: I just discovered that set /a does not require expanded variables and so removed the setlocal and endlocal commands and altered the set /a syntax.
after this line in environment a has the pid of the process sought
for /F "tokens=1,2,*" %%a in ('tasklist /fi "imagename eq %procName%"') do if !%%a!==!%procName%! set a=%b
I think I found a solution:
tasklist /fi "imagename eq %procName%" 2>nul | findstr /i %procName% | find /c /v "">proc_temp
set /p current_num= < proc_temp
echo !current_num!
Also I think the code can be simplified. Hope some of you can give brief version :)
I am trying to search for a process on a remote machine and pass an exit code so it can be handled by another process. So if the process exists output exit code 1, if not do nothing.
I wrote the script below with some help from another post. It works for a localmachine/local process but returns nothing for a remote process/machine. In the script below if i use the standalone tasklist command it works.
#echo off
setlocal enableDelayedExpansion
set "cmd=tasklist.exe /NH /s RemoteMachine /u RemoteMachine\administrator /p Password /fi "Imagename eq Install.exe""
for /F "delims=*" %%p in ('!cmd! ^| findstr "Install.exe" ') do (
echo exit 1
)
I would advise you to change the logic behind returning exit codes. Windows commands like FINDSTR use a different logic for that: if there's a match, the exit code is 0, and if there's no match, it's 1. And because the logic is already implemented in FINDSTR, you could just use it:
#tasklist.exe /NH /s RemoteMachine /u RemoteMachine\administrator /p Password /fi "Imagename eq Install.exe" | findstr "Install.exe" >nul