0

Using information from the answer below, I tried

set curlsnum=tasklist /FI "IMAGENAME eq curl.exe" 2>NUL | find /I /C "curl.exe"
echo %curlsnum%

and this returns

set curlsnum=tasklist /FI "IMAGENAME eq curl.exe" 2>NUL | find /I /C "curl.exe"
0

echo
ECHO is on.

So it counts it properly, but for some reason the zero doesn't make it into the variable. Set /a doesn't make any difference either. I must be missing something obvious but can't work out what. Do variables have to be natural numbers or something weird?!

How to count amount of processes with identical name currently running, using a batchfile

Ne Mo
  • 198
  • 4
  • 18
  • 1
    Did you try `echo %curlsnum%` I bet it's not what you expect. [Get output of command to a variable](https://stackoverflow.com/search?q=+%5Bbatch-file%5D+get+output+of+a+command+to+a+variable) – Stephan May 30 '22 at 10:46
  • Hi, yeah it's in the top codeblock. On the bottom we can see it just returns "echo", which tells me whether echo is currently on or off. In other words it doesn't return a zero - it doesn't return anything at all - and that's what I don't understand. – Ne Mo May 30 '22 at 11:02
  • ok, what's the output of `set curlsnum=tasklist /FI "IMAGENAME eq curl.exe" 2>NUL`? So, how many `curl.exe` could you expect? – Stephan May 30 '22 at 11:14
  • There can be used `for /F %%I in ('%SystemRoot%\System32\tasklist.exe /FI "IMAGENAME eq curl.exe" /NH ^| %SystemRoot%\System32\find.exe /C "curl.exe"') do set "curlsnum=%%I"` to get the number of running `curl.exe` tasks output by `find` assigned to an environment variable with name `curlsnum`. Run in a command prompt window `for /?` and `tasklist /?` and `find /?` and `set /?` for the usage helps of the four [Windows commands](https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/windows-commands) used here. – Mofi May 30 '22 at 11:29

2 Answers2

1

set sets a variable to a string. If that string happens to be a command, the variable will contain the command, not its output.

To catch the output to a variable in batch, you need a for /f loop (see for /?):

for /f %%a in ('tasklist /FI "IMAGENAME eq curl.exe" 2^>NUL ^| find /I /C "curl.exe"') do set "curlsnum=%%a"
echo %curlsnum%
Stephan
  • 53,940
  • 10
  • 58
  • 91
  • `2^>NUL` is not really necessary as long as the arguments are correct which is the case here with `/FI "IMAGENAME eq curl.exe"`. The information message "*INFO: No tasks are running which match the specified criteria.*" is output by __TASKLIST__ to __STDOUT__ on no matching task found. I have made in the past quite often the same "mistake" which is not really a mistake and now my template for __TASKLIST__ usage is correct defined in used text editor. – Mofi May 30 '22 at 11:28
  • Thanks. I had a feeling it was something like this but I had a lot of lines like *set /a totalpages = %totalrecords%/%pagesize%*, so I didn't quite get that some commands need something different. – Ne Mo May 30 '22 at 11:33
0

You can also try with powershell into a batch file like this :

@echo off
Title count instances of process
Set MyProcess=curl
Call :Count_Process "%Myprocess%" Count
echo There are %Count% of "%Myprocess%"
pause
Exit /B
::-------------------------------------------------------------------
:Count_Process <ProcessName> <Count>
@for /f "delims=" %%# in (
    'PowerShell -C "(Get-Process | Where {$_.Name -ieq '%1'}).count"'
) do Set "%2=%%#
Exit /B
::-------------------------------------------------------------------
Hackoo
  • 18,337
  • 3
  • 40
  • 70