2

I'm using the following to ping a list of computers from a text file (shown below)

I'm wondering if there is a way to add a count to the end of this. IE there are 20 OK machines and 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 Answers 2

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
0

not tested:

@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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.