如何在bat文件末尾添加一个计数

How to add a count to the end of a bat file

我正在使用以下命令从文本文件(如下所示)ping 计算机列表

我想知道是否有办法在最后添加一个计数。 IE 有 20 台 OK 机器和 50 台 FAILED。

@Echo OFF


For /F "Usebackq Delims=" %%# in ( "location of .txt file"
) do (
    Echo+
    Echo [+] Pinging: %%#

    Ping -n 1 "%%#" 1>nul && (
        Echo     [OK]) || (
        Echo     [FAILED])

)>>results.txt

Pause&Exit

未测试:

@Echo OFF


For /F "Usebackq Delims=" %%# in ( "location of .txt file"
) do (
    Echo+
    Echo [+] Pinging: %%#

    Ping -n 1 "%%#" 1>nul && (
        Echo     [OK]) || (
        Echo     [FAILED])

)>>results.txt

for /f "tokens=2 delims=: " %%a in ('find /i /c "[ok]" results.txt"') set "OK=%%a"
for /f "tokens=2 delims=: " %%a in ('find /i /c "[failed]" results.txt"') set "failed=%%a"

echo failed %failed%>>results.txt
echo ok %ok%>>results.txt

Pause&Exit
@Echo OFF
    Setlocal EnableExtensions DisableDelayedExpansion

    Set "up=0"
    Set "down=0"

(
    For /F "Usebackq Delims=" %%# in ( "location of .txt file"
    ) do (
        Echo+
        Echo [+] Pinging: %%#

        Ping -n 1 "%%#" 1>nul && (
            Set /a "up+=1"   & Echo     [OK]) || (
            Set /a "down+=1" & Echo     [FAILED])

    )

    Setlocal EnableDelayedExpansion
    Echo Up   : !up!
    Echo Down : !down!
    Endlocal

)>>results.txt

Pause&Exit