Search code examples
batch-fileping

Batch file - Need to take input and append input (D or L) to each line of text to perform Ping on


I currently have this script to ping each network name in a file provided to me (1 per line) and output result and device name to a log file and it's working fine.

Set "ComputerList=C:\computers.txt"  ***currently have to run twice with D or L appended to ensure I identify all desktop or laptops
Set "LogFile=C:\PingResults.txt"

If Not Exist "%ComputerList%" Exit /B
>"%LogFile%" (For /F UseBackQ %%A In ("%ComputerList%"
) Do Ping -n 1 %%A|Find "TTL=">Nul&&(Echo Yes [%%A])||Echo No [%%A])

But now I would like to take input from a prompt to specify whether I'm searching for Desktops (D) or Laptops (L) and append that value to each entry in the %ComputerList% (in our environment all desktops names are D + service tag and laptops are L + service tag, e.g. D123abc, or L123abc), everything else should be the same.

Any input would be greatly appreciated.

I am looking for advice on how to implement. I haven't attempted it myself yet.


Solution

  • @ECHO OFF
    SETLOCAL
    Set "ComputerList=U:\q75523666.txt"
    Set "LogFile=U:\PingResults.txt"
    Set "TempFile=U:\Temporary.txt"
    
    DEL "%tempfile%" 2>NUL
    
    >"%LogFile%" (
    FOR /f "tokens=1,2" %%b IN (%computerlist%) DO (
     SET "group="
     IF "%%c"=="" (
      FOR %%t IN (D L) DO (
       PING -n 1 %%t%%b|Find "TTL=">NUL
       IF NOT ERRORLEVEL 1 (
       Echo Yes [%%t%%b]
        >>"%tempfile%" ECHO %%b %%t
        SET "group=%%t"
       )
      )
      IF NOT DEFINED group (
       Echo No [?%%b]
       >>"%tempfile%" ECHO %%b
      )
     ) ELSE (
      PING -n 1 %%c%%b|Find "TTL=">Nul&&Echo Yes [%%c%%b]||Echo No [%%c%%b]
      >>"%tempfile%" ECHO %%b %%c
     )
    )
    rem ==== This is the beginning of a test routine for testing on MY system 
    rem ==== where the PING would always fail.
    FOR /f "tokens=1,2" %%b IN (%computerlist%) DO (
     SET "group="
     IF "%%c"=="" (
      IF "%%b"=="7v858c3" (
       Echo Oui [X%%b]
       >>"%tempfile%" ECHO %%b X
       SET "group=X"
      )
      IF NOT DEFINED group (
       Echo Non [?%%b]
       >>"%tempfile%" ECHO %%b
      )
     ) ELSE (
      Echo Oui/Non [%%c%%b]
      >>"%tempfile%" ECHO %%b %%c
     )
    )
    rem ==== This is the END of a test routine for testing on MY system 
    rem ==== Remove the code between the two "REM" blocks
    )
    
    MOVE "%tempfile%" "%computerlist%" >nul
    

    Notes

    I've changed filenames to suit my system. I've included a test block that I used for debugging. This should be removed - it's there to show how I tested the routine since "ping" on your taglist will always fail on my system.

    Read each line from the computerlist file. Use for /f (see for/? from the prompt or hundreds of examples on SO) to assign the tag to %%b and the group (D or L) to %%c

    if "%%c" is empty, then we have not determined the group, so try the ping... with %%t=D or L prepended to the tag. If found, report "Yes" on the logfile and record "tag group" on the tempfile. If not, report "No" and record "group" only.

    If %%c is not empty, test %%c%%bto produce the report and record "tag group" on the tempfile.

    When computerlist has been fully processed, a new version will appear in tempfile, so move tempfile over computerlist.

    Caution

    During your obligatory testing, it would be best to remark-out this move line (rem move...) and type "%tempfile%" to ensure that it contains the expected data which should be a list of tags with L or D appended if it has been determined that the tag belongs to a Laptop/Desktop OR the tag alone which indicates that it has not been determined whether the tag belongs to either group.