Page 1 of 1

How to print to first line of txt file?

Posted: 21 Feb 2020 00:46
by Whooops
I trying to make an small ping log to check when the IP isn't reachable but only issue is that all info goes down to the bottom of the file. Is it possible to print it to the top?

My script so far:

@ECHO OFF
:LOOPSTART
date /T>> PingLog.txt
time /T >> PingLog.txt

ping 192.168.0.2 -n 4 >> PingLog.txt
TIMEOUT 10
GOTO LOOPSTART

Re: How to print to first line of txt file?

Posted: 21 Feb 2020 01:55
by jfl
For every line you'd need to do some complex manipulation, like:

Code: Select all

WriteLine > PingLog2.txt
type PingLog.txt >> PingLog2.txt
del PingLog.txt
ren PingLog2.txt PingLog2.txt
Is this worth it?

Instead, I suggest that you consider using a Windows port of the Unix tail tool.
For example get tail.exe from UnxUtils.zip

Example of use:

Code: Select all

tail -5 PingLog.txt
will always show you the last 5 lines

Re: How to print to first line of txt file?

Posted: 25 Feb 2020 00:06
by AiroNG
Depending on the formatting of the your date/time stamps you could use the SORT command.