0

I'm pretty new to this so please bear with me, and if you require anymore information from me please say. Thanks in advance for your help.

I have this code that pings different PCs then returns back if they are online/offline. I wanted to know if you could add another column once the bat file has ran its ping test so it has the computer name next to it.

 @echo off
 if exist C:\tools\computers.txt goto Label1 
 echo.
 echo Cannot find C:\tools\computers.txt
 echo.
 Pause 
 goto :eof

:Label1 
 echo PingTest executed on %date% at %time% > C:\tools\z.txt
 echo ================================================= >> C:\tools\z.txt
 for /f %%i in (C:\tools\computers.txt) do call :Sub %%i notepad C:\tools\z.txt 
 goto :eof

:Sub
echo Testing %1 set state=alive ping -n 1 %1 | find /i "bytes=" || set state=dead echo %1 is %state% >> C:\tools\z.txt

The bat file creates a document that shows the following;

PingTest executed on 28/07/2016 at 13:10:28

99.1.82.28 is alive

99.1.82.100 is alive

ect.


If possible I would like the bat file to run so it displays this;

The bat file creates a document that shows the following;

PingTest executed on 28/07/2016 at 13:10:28

Computer 1 : 99.1.82.28 is alive

Computer 2 : 99.1.82.100 is alive

ect.

--

Would appreciate any help & guidance on this.

Thanks.

Hackoo
  • 18,337
  • 3
  • 40
  • 70
  • Take a look at this code ==> http://stackoverflow.com/questions/12408673/how-to-ping-multiple-servers-and-return-ip-address-and-hostnames-using-batch-scr?answertab=active#tab-top – Hackoo Jul 28 '16 at 15:27

1 Answers1

0

You can try this solution :

@echo off
Title Ping Test
set "URLS=URLS.txt"
set "LogFile=PingResults.txt"
If exist %LogFile% Del %LogFile%
(
    echo ******************************************************
    echo   PingTest executed on %Date% @ Time %Time% 
    echo ******************************************************
    echo(
) > %LogFile%

Setlocal EnableDelayedExpansion
for /f "usebackq delims=" %%a in ("%URLS%") do (
    for /f "tokens=2 delims=[]" %%b in ('ping -n 1 %%a') do set "ip=%%b"
        ping -n 1 %%a>nul && set "msg=%%a : !ip! ALive ok" || set "msg=%%a : !ip! Dead failed to respond"
        echo !msg!
        echo !msg! >> %LogFile%
    ) 
)
EndLocal
Start "" %LogFile%
pause>nul & exit

EDIT : on 29/07/2016 @ 12:48

Another version with multi-colors : Special thanks goes to ICARUS for the color function (-_°)

enter image description here

@echo off
Rem Special thanks goes to Iracus for the color function (-_°)
mode con cols=60 lines=20
Title Multi-Ping hosts Tester with Multi-colors by Hackoo
set "URLS=URLS.txt"
set "LogFile=PingResults.txt"
If exist %LogFile% Del %LogFile%
call :init
echo(
call :color 0E "------- Ping Status of Computers hosts -------" 1
echo(
(
    echo ******************************************************
    echo   PingTest executed on %Date% @ Time %Time% 
    echo ******************************************************
    echo(
) > %LogFile%
Setlocal EnableDelayedExpansion
for /f "usebackq delims=" %%a in ("%URLS%") do (
    for /f "tokens=2 delims=[]" %%b in ('ping -n 1 %%a') do set "ip=%%b"
        ping -n 1 %%a>nul && set "msg=%%a - !ip! ALive ok" && Call :Color 0A "!msg!" 1 || set "msg=%%a - !ip! Dead failed to respond" && Call :Color 0C "!msg!" 1
        echo !msg! >> %LogFile%
    ) 
)
EndLocal
Start "" %LogFile%
pause>nul & exit

:init
prompt $g
for /F "delims=." %%a in ('"prompt $H. & for %%b in (1) do rem"') do set "BS=%%a"
exit /b

:color
set nL=%3
if not defined nL echo requires third argument & pause > nul & goto :eof
if %3 == 0 (
    <nul set /p ".=%bs%">%2 & findstr /v /a:%1 /r "^$" %2 nul & del %2 2>&1 & goto :eof
) else if %3 == 1 (
    echo %bs%>%2 & findstr /v /a:%1 /r "^$" %2 nul & del %2 2>&1 & goto :eof
)
exit /b

EDIT : Update On 23/08/2016

http://pastebin.com/zjYwSqUM

Community
  • 1
  • 1
Hackoo
  • 18,337
  • 3
  • 40
  • 70