windows - 如何在bat文件末尾添加计数

标签 windows batch-file cmd

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

我想知道是否有办法在其末尾添加计数。 IE 有 20 台正常机器和 50 台失败机器。

@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
    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

关于windows - 如何在bat文件末尾添加计数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29948885/

相关文章:

batch-file - 管道查找findstr的输入

batch-file - 如果在文本文件列表中找不到文件,批处理文件是否可以删除文件?

windows - 如何使用forfiles删除不带扩展名的文件

windows - 如何在 Windows 中连接到 MongoDB?

windows - 如何在Windows中找到特定线程的入口点?

c++ - 为什么不是我可以加载(打开)的所有 DLL 模块?

powershell - 相当于 Windows cmd.exe 批处理文件的 bash "set -o errexit"?

C++/wcout/UTF-8

batch-file - 理解批处理文件中的start、2>nul、cmd等符号

arrays - 如何将参数从批处理文件传递到perl中的数组?