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

4
  • 1
    Did you try echo %curlsnum% I bet it's not what you expect. Get output of command to a variable Commented May 30, 2022 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. Commented May 30, 2022 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? Commented May 30, 2022 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 used here. Commented May 30, 2022 at 11:29

2 Answers 2

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%
Sign up to request clarification or add additional context in comments.

2 Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.