如何将计数添加到bat文件的末尾

时间:2015-04-29 15:58:29

标签: windows batch-file cmd

我使用以下命令从文本文件中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

2 个答案:

答案 0 :(得分:2)

@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

答案 1 :(得分:0)

未经测试:

@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