HDemaiter
HDemaiter

Reputation: 27

Batch script only takes first entry of a list located in .txt in loop

@echo off
    setlocal enabledelayedexpansion
    set OUTPUT_FILE=result.csv

    >nul copy nul %OUTPUT_FILE%
    echo HOSTNAME,LONGNAME,IPADDRESS,STATE >%OUTPUT_FILE%
    for /f "tokens=1 delims= " %%i in (hostnames.txt) do (
        set SERVER_ADDRESS_I=UNRESOLVED
        set SERVER_ADDRESS_L=UNRESOLVED
        for /f "tokens=1,2,3" %%x in ('ping -n 1 %%i ^&^& echo SERVER_IS_UP') do (
        if %%x==Pinging set SERVER_ADDRESS_L=%%y
        if %%x==Pinging set SERVER_ADDRESS_I=%%z
            if %%x==SERVER_IS_UP (set SERVER_STATE=UP) else (set SERVER_STATE=DOWN)
        )
        echo %%i [!SERVER_ADDRESS_L::=!] !SERVER_ADDRESS_I::=! is !SERVER_STATE!
        echo %%i,!SERVER_ADDRESS_L::=!,!SERVER_ADDRESS_I::=!,!SERVER_STATE!
>>%OUTPUT_FILE%
    )

So i use this script to ping every hostname from a list of hostnames and make a list with attributes. The problem is the list formatting changed from every hostname sepperated by a newline to everyhostname sepparated by a space. now this script only takes the first hostname from the list. How to change the script so that it takes all hostnames? (I messed a bit with the tokens and delims but wasn't able to make it work)

Upvotes: 0

Views: 95

Answers (1)

woxxom
woxxom

Reputation: 73526

Read the file contents into a variable and use plain for to enumerate space-separated tokens:

set /p hostnames=<hostnames.txt
for %%i in (%hostnames%) do (
   ........
)

Upvotes: 1

Related Questions