Ping ip address and save result to txt file.
8,150
@echo off
SET LOGFILE=C:\Temp\MyLogFile.log
SET PINGLIST=C:\Users\kelly\Desktop\Ping\computerlist.txt
for /f "delims=" %%a in (%PINGLIST%) DO ping -n 1 %%a > nul && (echo %%a is ok >> %LOGFILE%) || (echo %%a is unreachable! >> %LOGFILE%)
Just make sure your computer list only has one hostname on each line.
Input (computerlist.txt)
gooobler
google.com
msn.com
localhost
Output (MyLogFile.log)
gooobler is unreachable!
google.com is ok
msn.com is unreachable!
localhost is ok
Related videos on Youtube
Comments
-
Kelly over 1 year
I am trying to open a list of ip address and ping them and then save the responce to a TXT file. The ping loops through and runs with out a problem and reports correctly but i can get it to save the results to a text file.
@echo off SET LOGFILE=MyLogFile.log call :Logit >> %LOGFILE% exit /b 0 for /f "delims=" %%a in ( ' type "C:\Users\kelly\Desktop\Ping\computerlist.txt" ' ) do ping -n 1 %%a >nul && (echo %%a ok >> %LOGFILE% ) || (echo %%a failed to respond >> %LOGFILE% ) pause
-
sippybear over 5 yearsIs this an excerpt from your batch file? I don't see a label for Logit, just a call statement.
-
Kelly over 5 years@sippybear no this is my whole scrip. I probably missed something I dont normally build bat files so i was googling and putting stuff together. Everything was good till to point of adding the results to the txt file.
-
-
Kelly over 5 yearsSo i now have this.
@echo off SET LOGFILE=C:\Users\kelly\Desktop\Ping\MyLogFile.log SET PINGLIST=C:\Users\kelly\Desktop\Ping\computerlist.txt for /f "delims=" %%a in (%PINGLIST%) do ping -n 1 %%a > %LOGFILE% && (echo %%a is ok >> %LOGFILE%) || (echo %%a is unreachable! >> %LOGFILE%)
It works however it only writes to the first line and if two fail then it only shows the last one to be ran. I want to to show if it failed of if it was successful on all of them. -
Kelly over 5 yearsGot it to work i had to run it as administrator
-
mshafer over 5 yearsUmmm, I think that you want to make the
ping -n 1 %%a > %LOGFILE%
an append instead (ping -n 1 %%a >> %LOGFILE%
) but I don't think this solves all your issues. Because ping returns 0 (considers it a success as long as it gets any response even from your dns saying the destination is unreachable). -
mshafer over 5 years@Kelly I updated my post. You'll need to tweak the file paths to what you want to use, but I think it will work for you. (If you may have spaces in the paths, you'll also need to put quotes around the variables when they are used).