Tasklist total memory of specific proces with subprocesses windows - batch-file

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"
...

Related

Is there a way to do this less time consuling within one check?

I have this batch code and its working perfectly well, BUT:
every line take ~60 seconds to execute and I'm wondering, if I could do this faster...
Also the if Statements.. I want to check, if every Window is closed, and if so, I want to execute something. But if at least one window is still open, then it should check it again.
:loop
for /f %%i in ('tasklist /v /fi "WINDOWTITLE eq %server1%" /FO Table') do set #1=%%i
for /f %%i in ('tasklist /v /fi "WINDOWTITLE eq %server2%" /FO Table') do set #2=%%i
for /f %%i in ('tasklist /v /fi "WINDOWTITLE eq %server3%" /FO Table') do set #3=%%i
for /f %%i in ('tasklist /v /fi "WINDOWTITLE eq %server4%" /FO Table') do set #4=%%i
for /f %%i in ('tasklist /v /fi "WINDOWTITLE eq %server5%" /FO Table') do set #5=%%i
for /f %%i in ('tasklist /v /fi "WINDOWTITLE eq %server6%" /FO Table') do set #6=%%i
if not %#1%==cmd.exe (
if not %#2%==cmd.exe (
if not %#3%==cmd.exe (
if not %#4%==cmd.exe (
if not %#5%==cmd.exe (
if not %#6%==cmd.exe (
goto backup
)
)
)
)
)
) else (
echo back to loop
goto openWindow
)
Let me suggest a slightly different approach. Instead of all those if statements, you can just loop whenever one of the tasks exist:
:wait
timeout 1 >nul
for %%a in (%server1% %server2% %server3% %server4% %server5% %server6%) do (
tasklist /nh /fi "windowtitle eq %%a" |find " " >nul && goto :wait
)
echo all closed.
or
setlocal enabledelayedexpansion
:wait
timeout 1 >nul
for /l %%a in (1,1,6) do (
tasklist /nh /fi "windowtitle eq !server%%a!" |find " " >nul && goto :wait
)
echo all closed.
Note: find " " looks for two consecutive spaces, not a TAB)
If you choose your window titles wisely, you don't even need a for loop:
:wait
timeout 1 >nul
tasklist /nh /fi "windowtitle eq MySubWindow*" |find " " >nul && goto :wait
echo all closed.
where the window titles all start with a fixed string (MySubWindow here), like MySubWindow-1, MySubWindow-2 etc. (yes, tasklist is able to use a wildcard - but only at the end of the string). This is basically "if any window exists with a title that starts with MySubWindow then loop"
little optimization of Stephan answer.
This execute faster.
:wait
timeout 1 >nul
for %%a in (%server1% %server2% %server3% %server4% %server5% %server6%) do (
if not defined v%%a tasklist /nh /fi "windowtitle eq %%a" |find " " >nul && goto :wait
set v%%a=done
)
echo all closed.
You have not explained how your 6 Batch files are "open", but if they are open via START command, then there is a much simpler way to do the same:
(
start call batch1.bat
start call batch2.bat
start call batch3.bat
start call batch4.bat
start call batch5.bat
start call batch6.bat
) | pause
echo All 6 Batch files are closed
The previous code run the 6 Batch files in parallel and then the control flow is stopped at the pause command. When all the 6 Batch files terminates, this program continue.
Note that there is not any Batch code that check if the procesess ends; this is done automatically by the Operating System. In this way, the wait state of this program does not waste any CPU time. For a further explanation, see this answer
Without being able to test this, perhaps it would be quicker to run just one tasklist command per loop:
#Echo Off
SetLocal EnableExtensions
:Loop
For /F Tokens^=17^ Delims^=^" %%G In (
'%SystemRoot%\System32\tasklist.exe /Fi "ImageName Eq cmd.exe" /Fo CSV /NH /V 2^>NUL'
) Do Set /P "=%%G" 0<NUL | %SystemRoot%\System32\findstr.exe /I ^
"\<%server1%\> \<%server2%\> \<%server3%\> \<%server4%\> \<%server5%\> \<%server6%\>" 1>NUL && GoTo Loop
:Backup
The findstr.exe options may need to be adjusted depending upon the content of those variables.

How to fix" ERROR-invalid argument/option - 'Live' " in this code?

So I want to close my live wallpaper when epic games launcher starts and then start it when i close the launcher and with other programs it worked fine but now it gives me this error message:ERROR: Invalid argument/option - 'Live'.
I think this is because the live wallpaper has spaces in its name, but I tried quotation marks and it still doesn't work. Do you know what I should do?
#echo off
:TEST
tasklist /nh /fi "imagename eq EpicGamesLauncher.exe" | find /i "EpicGamesLauncher.exe" >nul
if "%ERRORLEVEL%"=="1" goto TIMER
taskkill /im "DesktopHut Live v5.0.0.exe" /t /f
:TIMER
timeout /T 10
goto TEST2
:TEST2
tasklist /FI "IMAGENAME eq EpicGamesLauncher.exe" 2>NUL | find /I /N "EpicGamesLauncher.exe">NUL
if "%ERRORLEVEL%"=="0" goto TEST
tasklist /nh /fi "imagename eq "DesktopHut Live v5.0.0.exe" "| find /i /n "DesktopHut Live v5.0.0.exe" >nul
if "%ERRORLEVEL%"=="0" goto TEST
start " " "C:\Users\David\Desktop\DesktopHut Live v5.0.0.exe"
goto TEST
pause>nul
The reason for the error message is that you've used a wrong syntax for the filter expression of tasklist: tasklist /nh /fi "imagename eq "DesktopHut Live v5.0.0.exe" "
The filter expressions can have spaces in them and tasklist does not have any problems with them, The real problem is the limitation in the output of tasklist which is apparently fixed at 25 characters for the Image Name column, when using the default output formatting.
So assuming the process DesktopHut Live v5.0.0.exe is already running, The output of
tasklist /fi "imagename eq DesktopHut Live v5.0.0.exe"
would be:
Image Name
=========================
DesktopHut Live v5.0.0.ex
To overcome that limitation you can specify csv as the output format by the /fo csv switch
tasklist /fi "imagename eq DesktopHut Live v5.0.0.exe" /fo csv | find /i "DesktopHut Live v5.0.0.exe" >nul
Since you are using find to set the errorlevel, there is no need to use a filter expression for tasklist so the entire command can be simplified to:
tasklist /fo csv | find /i "DesktopHut Live v5.0.0.exe" >nul

How to find processes with identical name currently running, using a batchfile

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)

Wildcard in taskkill windowtitle

When I want to close the following two (fictional...) applications using taskkill...
...I would use taskkill /FI "WINDOWTITLE eq Hello*".
But how about these two:
taskkill /FI "WINDOWTITLE eq * wine" gives me FEHLER: Der Suchfilter wurde nicht erkannt., which can be translated as ERROR: The search filter could not be recognized.
So, how can I filter with a wildcard at the beginning?
The wildcard at the beginning does not work. You would need to incorporate findstr using a bit of initiative.
for /f "tokens=2 delims=," %%a in ('tasklist /fi "imagename eq notepad.exe" /v /fo:csv /nh ^| findstr /r "wine"') do taskkill /pid %%a
So we search for imagenames with wine in the name. Use /fo to csv format, /nh for no header, then search for the string "wine" in imagename, then kill by process ID if found.
To not be imagename specific do:
for /f "tokens=2 delims=," %%a in ('tasklist /v /fo:csv /nh ^| findstr /r "wine"') do taskkill /pid %%a
Edit
As for the concern in killing incorrect tasks:
#echo off
set "images=notepad.exe,calc.exe,winword.exe,excel.exe"
for %%i in (%images%) do (
for /f "tokens=2 delims=," %%a in ('tasklist /fi "imagename eq %%i" /v /fo:csv /nh ^| findstr /r "wine"') do taskkill /pid %%a
)
Just add a list of possible image names that would contain the title, it will only loop these as per below and not touch the other processes/tasks:
tasklist /fi "imagename eq notepad.exe"
tasklist /fi "imagename eq calc.exe"
tasklist /fi "imagename eq winword.exe"
tasklist /fi "imagename eq excel.exe"

Compare number of a specific process to a number

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 :)

Resources