Windows Ping with timestamp

Time:2020-10-21

usage method

Create a new text file, change the suffix to. Bat, edit the file, copy the following code in, Ctrl + s save it, double-click the file to run
design sketch

Operation effect:

Ping the target network once a second,
Create Ping_ host.log File save records, will be covered, meaning that you run this script twice Ping the same host, the second time the file will cover the first

Suspension mode:

The first one, Ctrl + C, asks if you want to stop typing y, enter
Second, close the window directly

result:

You can see that a. Log file is generated in the current directory, which is actually a TXT text file, but the suffix is different. You can open the file in Notepad

This is the time stamped record I just pinged

Simplified code

@echo off
set /p host=host:
set logfile=ping_%host%.log
echo Target Host=%host% > %logfile%

:loop
for /f "tokens=* skip=2" %%A in ('ping %host% -n 1') do (
	echo %date:~0,-3% %time:~0,-3% %%A>>%logfile%
	echo %date:~0,-3% %time:~0,-3% %%A
	timeout /t 1 /nobreak>nul
	goto loop
)
pause>nul

Annotated version code

A little wordy do not understand specific instructions can baidu

:: This is the comment file name test.bat
@echo off
:: turn off echo

set /p host=myhost:
: This is a note / P effect requires the user to enter host, which is the address you want to Ping
: after setting string variable =: prompt before

set logfile=ping_%host%.log
:: set the file name. To reuse the variable set by set, host needs to wrap% on both sides

echo Target Host=%host% > %logfile%
: echo output string > to input object
::      > file.txt  Output string to file; no > default output to screen; > nul output to empty object, the prompt statement of instruction operation will not be displayed


:: enhance for loop
: tokens select the divided part of a row * all the rest 
:: skip skips the first few lines
::% date: ~ 0, - 3% date string from the first to the last third
: > > a.txt is appended to a.txt
: > a.txt no a.txt is created, but a.txt is cleared
:: echo STR output string
:: echo STR > > filename output string to file
: timeout / T waittime / nobreak > nul wait time cannot stop during waittime > nul means do not print to the command window does not display
:: ": loop" "goto loop" loop
: pause > nul press any key to continue without a prompt

:loop
for /f "tokens=* skip=2" %%A in ('ping %host% -n 1') do (
	echo %date:~0,-3% %time:~0,-3% %%A>>%logfile%
	echo %date:~0,-3% %time:~0,-3% %%A
	timeout /t 1 /nobreak>nul
	goto loop
)
pause>nul