How to print to first line of txt file?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Whooops
Posts: 1
Joined: 21 Feb 2020 00:42

How to print to first line of txt file?

#1 Post by Whooops » 21 Feb 2020 00:46

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

jfl
Posts: 226
Joined: 26 Oct 2012 06:40
Location: Saint Hilaire du Touvet, France
Contact:

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

#2 Post by jfl » 21 Feb 2020 01:55

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

AiroNG
Posts: 46
Joined: 17 Nov 2013 15:00
Location: Germany

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

#3 Post by AiroNG » 25 Feb 2020 00:06

Depending on the formatting of the your date/time stamps you could use the SORT command.

Post Reply