I have two batch files to ping all IPs in a text file servers.txt
.
One of the batch files pings the servers and shows the result on the CMD window.
The other batch file pings the servers and shows nothing on CMD windows and after it finished all the pings to servers , it will put them in the OnlineServers.txt
file.
I want to mix this thing.
I want the batch file to run pings and show them on screen and put all the online servers in the OnlineServers.txt
.
Here is first batch file which shows pings on CMD windows without any output to text file :
@echo off
for /f "delims=" %%a in (servers.txt) do ping -n 1 %%a >nul && (echo %%a Online) || (echo %%a Offline)
echo.
pause
And here is the second batch file which shows nothing on CMD window and only output the file after it pings all the servers :
@echo off
setlocal EnableDelayedExpansion
(for /F "delims=" %%a in (servers.txt) do (
ping -n 1 "%%a" > NUL
if !errorlevel! equ 0 (
echo %%a Online
)
)) > OnlineServers.txt
These is more than 150 servers to check and I add servers to this list every day so its a long list to check.
If you want to add some colors for your batch file like that :
So you can get as output like this one :
The batch writing to the file will have to breakoff the enclosing parentheses with the redirection and reset the file at start.
You could simply implement explicit redirection to the
con
device (console).By the way, you actually do not need delayed expansion if you use the
if ErrorLevel
syntax: