Hello, I've been trying to do this for a long time without success.
Basically,
1. I want to watch a txt file for changes (new lines really)
2. Copy New line into Another .txt file and save it
I just need simple template code to get me on the right track.
Watch .txt file for changes, copy the New line..
- Spiderkeys
- Posts: 31
- Joined: 31 Dec 2015, 07:24
-
- Posts: 1472
- Joined: 05 May 2018, 12:23
Re: Watch .txt file for changes, copy the New line..
if its new lines and not new text anywhere see
https://autohotkey.com/board/topic/54538-monitor-new-lines-in-text-files/
especially this https://autohotkey.com/board/topic/6416-tail-the-last-lines-of-a-text-file/
https://autohotkey.com/board/topic/54538-monitor-new-lines-in-text-files/
especially this https://autohotkey.com/board/topic/6416-tail-the-last-lines-of-a-text-file/
- Spiderkeys
- Posts: 31
- Joined: 31 Dec 2015, 07:24
Re: Watch .txt file for changes, copy the New line..
Thanks I will so far check out the first link, the other link doesn't apply to me because a new line it not always the last line.
- Spiderkeys
- Posts: 31
- Joined: 31 Dec 2015, 07:24
Re: Watch .txt file for changes, copy the New line..
Ok, i've actually already seen this thread many times with a google search.
This part is ok
Somehow I'm on the right track, it detect changes, and gives me a msgbox telling me, but I would like this to Copy the newly added line and paste the new line into another txt file.
This part is ok
Code: Select all
#Persistent
MonitoredFile := A_DeskTop "\test.txt"
SetTimer, AttribChanged, 2000
Return
AttribChanged:
FileGetAttrib, Attrib, %MonitoredFile%
IfInString, Attrib, A
{
FileSetAttrib,-A, %MonitoredFile%
;Here comes your code ... for example:
MsgBox, The File %MonitoredFile% has changed
}
Return
-
- Posts: 1472
- Joined: 05 May 2018, 12:23
Re: Watch .txt file for changes, copy the New line..
how many lines is your file on average?
- Spiderkeys
- Posts: 31
- Joined: 31 Dec 2015, 07:24
Re: Watch .txt file for changes, copy the New line..
I'm monitoring a configuration file (cfg) file, which has nearly 50,000 lines and growing and if it adds a entry it places the line elsewhere in the file and not at the bottom of the document, there are a few real-time log monitoring programs like mtail, and even Notepad++ but they only seem to monitor the bottom of the text file and only wait for the new lines there, if a new line gets inserted in the middle of document, I get alerted with false results coming the bottom of file.
Re: Watch .txt file for changes, copy the New line..
You can build an associative array, use the line content as key, the value can be left empty.
When you get alerted of a change, you loop through the lines of the changed files, haskey tells you if the line was already present in the old file.
It's a bit more complicated if there duplicate lines can occur, in that case you can store as "value" of the array element the number of occurrences of the line in the old file and the newly found number, so your associative array looks like {(firstline):[oldnumber, foundnow], {secondline:[oldnumber, foundnow],... }. If you find more duplicates than there were in the old file, one of them is new.
If that is not performant enough, there is a class that implements a hashed array - I have not used it yet, but it has been mentioned in several forum posts.
When you get alerted of a change, you loop through the lines of the changed files, haskey tells you if the line was already present in the old file.
It's a bit more complicated if there duplicate lines can occur, in that case you can store as "value" of the array element the number of occurrences of the line in the old file and the newly found number, so your associative array looks like {(firstline):[oldnumber, foundnow], {secondline:[oldnumber, foundnow],... }. If you find more duplicates than there were in the old file, one of them is new.
If that is not performant enough, there is a class that implements a hashed array - I have not used it yet, but it has been mentioned in several forum posts.
Last edited by braunbaer on 30 Jul 2021, 06:34, edited 1 time in total.
-
- Posts: 1472
- Joined: 05 May 2018, 12:23
Re: Watch .txt file for changes, copy the New line..
The problem is how does he even locate the new line to check the haskey?braunbaer wrote: ↑30 Jul 2021, 06:22You can build an associative array, the key is the line, the value can be left empty.
When you get alerted of a change, you loop through the lines of the changed files, haskey tells you if the line was already present in the old file.
If that is not performant enough, there is a class that implements a hashed array - I have not used it yet, but it has been mentioned in several forum posts.
The software he's using if this is a regular text based cfg file has to be going through a lot of trouble to not appended the new data at the end.
Re: Watch .txt file for changes, copy the New line..
There is no other way then looping through the new file and checking every line, when something in the middle of the file was changed....
-
- Posts: 1472
- Joined: 05 May 2018, 12:23
Re: Watch .txt file for changes, copy the New line..
I suspect that the program generating this file is not really updating a cgf file but generating a while new one either on request or every so often. It is generating this file based on data it probably stores in a DB. Just guessing.
Re: Watch .txt file for changes, copy the New line..
That is quite possible, but does it make a difference? Anyway, if you insert text in the middle of a textfile, the rest of the file must be written again.
Who is online
Users browsing this forum: garry, marypoppins_1, mikeyww, OrangeCat, RussF and 151 guests