6

This batch file

@echo off
set path=C:\Users\ssiyengar\Desktop\Pingtest\pinglist.csv
set file=C:\Users\ssiyengar\Desktop\Pingtest\temp.txt
set qosping=1
cls
for /f "tokens=1-3 delims=," %%a IN (%path%) do (
    ping %%c -n %qosping% > %file%
    findstr "time< time=" %file% >nul
    if %errorlevel%==1 (
    echo %%a  %%b IP %%c Ping FAILURE
    ) else (
    echo %%a  %%b IP %%c Ping SUCCESS
    )
)
pause

outputs:

'ping' is not recognized as an internal or external command,
operable program or batch file.

'findstr' is not recognized as an internal or external command, operable program or batch file.

SYS1 MC1 IP XX.XX.XX.XX Ping SUCCESS

The CSV file is :

SYS1,MC1,IP1 \
SYS2,MC2,IP2

Why is ping/findstr not recognized?
If I directly pass the values instead of from the CSV file, it works fine. Why? How can I resolve it?
Note: The environment variables already contain the path of Sys32.

4 Answers4

14

The problem is that you set path. Path is a special environmental variable designed to hold paths to locations that have programs, such as ping and findstr.

by typing set path=... you overwriting this list with a file clearing the path for this time it runs. Luckily using set only changes the variable during this script's runtime and not for all sessions. This is why going to cmd and typing ping with the values from the csv is still working.

If you type the set path= line in that same cmd session, the manual typing ping with the values won't work anymore either until cmd is closed and reopened.

If you change: set path= to set mypath= and %path% to %mypath% your script will work.

LPChip
  • 66,193
4

In the system variables you already have a variable with the name path,
it points to several paths where the command interpreter () will
search and execute your commands, looking for files with the extensions
defined in PathExt, and when not finding, will return you:

  • 'ping' is not recognized as an internal or external command, operable program or batch file.
  • 'findstr' is not recognized as an internal or external command, operable program or batch file.

I suggest you know the names of the variables and start using different names.

Io-oI
  • 9,237
2

You can try like this and you don't need to create a temporary file either:


@echo off
Title Ping Tester
set "My_CSV_PingList=%~dp0pinglist.csv"
set qosping=1
set "LogFile=%~dp0PingResults.txt"
If exist "%LogFile%" Del "%LogFile%"
cls
SetLocal EnableDelayedExpansion
for /f "tokens=1-3 delims=," %%a IN (%My_CSV_PingList%) do (
    Ping -n %qosping% %%c |find "TTL=">nul) && (
        set "msg=%%a  %%b IP %%c Ping SUCCESS" && echo !msg!
        echo !msg!>>"%LogFile%"
     ) || (
        set "msg=%%a  %%b IP %%c Ping FAILURE" && echo !msg!
        echo !msg!>>"%LogFile%"
    )
)
EndLocal
Start "Log" /MAX "%LogFile%"
pause

Bonus: Refer to how to use multiple colors in batch. You can show the message with a green color when success and a red color when failure.


@echo off
Title Ping Tester With Powershell Foreground Colors In A Batch File
set "My_CSV_PingList=%~dp0pinglist.csv"
set qosping=1
set "LogFile=%~dp0PingResults.txt"
If exist "%LogFile%" Del "%LogFile%"
cls
SetLocal EnableDelayedExpansion
for /f "tokens=1-3 delims=," %%a IN (%My_CSV_PingList%) do (
    Ping -n %qosping% %%c |find "TTL=">nul) && (
        set "msg=%%a  %%b IP %%c Ping SUCCESS" && Call :PSColor "!msg!" Green \n
        echo !msg!>>"%LogFile%"
     ) || (
        set "msg=%%a  %%b IP %%c Ping FAILURE" && Call :PSColor "!msg!" Red \n
        echo !msg!>>"%LogFile%"
    )
)
EndLocal
Start "Log" /MAX "%LogFile%"
pause
Exit /B
::---------------------------------------------------------------
 :PSColor <String> <Color> <NewLine>
 If /I [%3] EQU [\n] (
     Powershell Write-Host "`0%~1" -ForegroundColor %2
 ) Else (
     Powershell Write-Host "`0%~1" -ForegroundColor %2 -NoNewLine
 )
 Exit /B
 ::--------------------------------------------------------------
Hackoo
  • 1,410
-2

Stop using the antiquated command prompt and use PowerShell instead.

$hosts = Import-CSV pinglist.csv
$hosts | Foreach-Object {
  $result = (Test-NetConnection -ErrorAction SilentlyContinue `
      -WarningAction SilentlyContinue -InformationLevel Quiet $_.IP) ? "SUCCESS" : "FAILURE"
  Write-Host ("{0} {1} IP {2} {3}" -f $_.Name,$_.mc,$_.IP,$result)
}

You need to add a header line to your CSV to get the attribute names per line:

Name,mc,IP