Watch .txt file for changes, copy the New line..

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
Spiderkeys
Posts: 31
Joined: 31 Dec 2015, 07:24

Watch .txt file for changes, copy the New line..

29 Jul 2021, 22:49

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.
User avatar
Spiderkeys
Posts: 31
Joined: 31 Dec 2015, 07:24

Re: Watch .txt file for changes, copy the New line..

30 Jul 2021, 00:23

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.
User avatar
Spiderkeys
Posts: 31
Joined: 31 Dec 2015, 07:24

Re: Watch .txt file for changes, copy the New line..

30 Jul 2021, 00:33

Ok, i've actually already seen this thread many times with a google search.

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
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.
AHKStudent
Posts: 1472
Joined: 05 May 2018, 12:23

Re: Watch .txt file for changes, copy the New line..

30 Jul 2021, 00:46

how many lines is your file on average?
User avatar
Spiderkeys
Posts: 31
Joined: 31 Dec 2015, 07:24

Re: Watch .txt file for changes, copy the New line..

30 Jul 2021, 05:14

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.
braunbaer
Posts: 478
Joined: 22 Feb 2016, 10:49

Re: Watch .txt file for changes, copy the New line..

30 Jul 2021, 06:22

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.
Last edited by braunbaer on 30 Jul 2021, 06:34, edited 1 time in total.
AHKStudent
Posts: 1472
Joined: 05 May 2018, 12:23

Re: Watch .txt file for changes, copy the New line..

30 Jul 2021, 06:32

braunbaer wrote:
30 Jul 2021, 06:22
You 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 problem is how does he even locate the new line to check the haskey?

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.
braunbaer
Posts: 478
Joined: 22 Feb 2016, 10:49

Re: Watch .txt file for changes, copy the New line..

30 Jul 2021, 06:36

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....
AHKStudent
Posts: 1472
Joined: 05 May 2018, 12:23

Re: Watch .txt file for changes, copy the New line..

30 Jul 2021, 06:44

braunbaer wrote:
30 Jul 2021, 06:36
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....
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.
braunbaer
Posts: 478
Joined: 22 Feb 2016, 10:49

Re: Watch .txt file for changes, copy the New line..

30 Jul 2021, 06:48

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.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: downstairs, LRRUNB, OrangeCat and 165 guests