如何计算批处理文件中的 curl 实例数并将其放入变量中?
How can I count the number of curl instances from a batch file and put it into a variable?
使用以下答案中的信息,我尝试了
set curlsnum=tasklist /FI "IMAGENAME eq curl.exe" 2>NUL | find /I /C "curl.exe"
echo %curlsnum%
和这个returns
set curlsnum=tasklist /FI "IMAGENAME eq curl.exe" 2>NUL | find /I /C "curl.exe"
0
echo
ECHO is on.
所以它正确地计算了它,但是由于某种原因零没有进入变量。 Set /a 也没有任何区别。我一定是遗漏了一些明显的东西,但无法弄清楚是什么。变量必须是自然数还是什么奇怪的东西?!
How to count amount of processes with identical name currently running, using a batchfile
您也可以尝试将 powershell 放入批处理文件中,如下所示:
@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
::-------------------------------------------------------------------
set
将变量设置为字符串。如果该字符串恰好是命令,则变量将包含 command,而不是其 output.
要批量捕获输出到变量,您需要一个 for /f
循环(参见 for /?
):
for /f %%a in ('tasklist /FI "IMAGENAME eq curl.exe" 2^>NUL ^| find /I /C "curl.exe"') do set "curlsnum=%%a"
echo %curlsnum%
使用以下答案中的信息,我尝试了
set curlsnum=tasklist /FI "IMAGENAME eq curl.exe" 2>NUL | find /I /C "curl.exe"
echo %curlsnum%
和这个returns
set curlsnum=tasklist /FI "IMAGENAME eq curl.exe" 2>NUL | find /I /C "curl.exe"
0
echo
ECHO is on.
所以它正确地计算了它,但是由于某种原因零没有进入变量。 Set /a 也没有任何区别。我一定是遗漏了一些明显的东西,但无法弄清楚是什么。变量必须是自然数还是什么奇怪的东西?!
How to count amount of processes with identical name currently running, using a batchfile
您也可以尝试将 powershell 放入批处理文件中,如下所示:
@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
::-------------------------------------------------------------------
set
将变量设置为字符串。如果该字符串恰好是命令,则变量将包含 command,而不是其 output.
要批量捕获输出到变量,您需要一个 for /f
循环(参见 for /?
):
for /f %%a in ('tasklist /FI "IMAGENAME eq curl.exe" 2^>NUL ^| find /I /C "curl.exe"') do set "curlsnum=%%a"
echo %curlsnum%