Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

help with a few lines of code, reading last line of txt file as the file updates


  • Please log in to reply
16 replies to this topic
joelreitzloff
  • Members
  • 12 posts
  • Last active: Aug 07 2016 02:30 AM
  • Joined: 28 Dec 2007

i wanna make a script but ive never used this function before. i want it to read the last line of C:\Users\Joel\Documents\Entropia Universe\chat.txt, and constantly moniter it and read each new line as it appears/updates (a game adds to it as i'm playing). the txt file is about 35megs, if that matters. i want it to hit print screen after a 500ms sleep if it sees "Dark Sleeper Horse" and to hit print screen after 1000ms sleep if it sees "Dark Sleeper Horse" and "Hall of Fame". i'm hoping the "Loop (read file contents) function can do this without bogging down my system. i have another script running a loop at 400ms and i noticed if i use 2 or 3 such scripts it bogs me down. but i read this function doesn't lag because it doesnt have to be rescanned constantly like FileReadLine. im not sure what a_LoopReadLine does either. im new to looking at files.

 

plz help. i assume it's just a few lines of code and it'd really make my day if someone could type it out for me so i can learn how it works



robert_ilbrink
  • Members
  • 561 posts
  • Last active: Mar 10 2015 08:36 PM
  • Joined: 05 May 2012

Try this....

#SingleInstance Force
#installKeybdHook
#Persistent
AppName=ReadLast
Menu, Tray, Tip, %AppName%
Menu, Tray, Icon , Shell32.dll, 14, 1
TrayTip, %AppName%, Started, 1

FileRead, AllLines, C:\Users\Joel\Documents\Entropia Universe\chat.txt   ; Read ALL lines into AllLines
StringGetPos, LastLineNr, AllLines,`n, R1, ; Get position of last line
StringTrimLeft, LastLine, AllLines, %LastLineNr% ; Remove everything before last line
StringReplace, LastLine, LastLine, `n ; Remove line break

MsgBox, %LastLine%
ExitApp


joelreitzloff
  • Members
  • 12 posts
  • Last active: Aug 07 2016 02:30 AM
  • Joined: 28 Dec 2007

i replace yourtxtfilename.txt with log.txt. i dont know where to fill in which dir the file is in. why the msg box?

 

um i put in the file name like i mentioned and tried running it and i just got ablank msg box then it ended. it seems like this checks the file once, i want it to constantly moniter the txt file and hit print screen each time a new line pops up. and like i said, when it sees a new line with it to hit print screen after 500ms if it sais 'dark sleeper horse' or to hit if after 1000ms if it has 'dark sleeper horse' combined with 'hall of fame'.

sry im still confused but i appreciate your help! once i get it right i will learn what most of it means grin.png



robert_ilbrink
  • Members
  • 561 posts
  • Last active: Mar 10 2015 08:36 PM
  • Joined: 05 May 2012

Wow, I had expected that you would be able to handle some basics of programming and I thought you only wanted to know how to quickly read the last line (kind of "tail" command) of a file, but it looks like you expect me to write everything....

 

I updated the file issue. You write that the log is about 35 MB, this size will impact the performance, smaller is better. Is there no way to make a dated copy and clean out the original one? If you want to read this file constantly to check for changes, and trigger scripts within a few hundred ms, this might be a challenge. You might want to look into either using tail compiled for DOS/Windows or maybe getting a file written signal through COM (e.g. looking at last changed time or even getting a file write stream....).



Person93
  • Members
  • 443 posts
  • Last active: Feb 11 2014 12:07 AM
  • Joined: 26 Jan 2012

To improve the speed at which the file is read, it may help to keep the file on a ramdisk.



joelreitzloff
  • Members
  • 12 posts
  • Last active: Aug 07 2016 02:30 AM
  • Joined: 28 Dec 2007

there are some other programs that do this but they crash a lot for me, but not for other users for some reason. the best one i've found crashes especially when i alt+tab for a minute then bring the game back up, which makes no sense because it's strictly supposed to use the log to comply with the game's rules. also i was thinking.. if a script has the file open, will the game still be able to constantly update it? Some of these lines come in several per second. But usually on average they're comming in once every few seconds. Anyways i'll want it to start at the last line and begin monitering when I start the script (hm hopefully if i open the game after the scipte it doesn't crash)

 

I will contact the makers of the other programs and ask what they used and how they managed this. they are players of the "game" so they may even be willing to share. also I plan to look everything up and learn about it that you guys give me, it's just a busy day today. also i assumed it'd be like < 10 lines so no one would mind. and i already learned some window manipulating commands today for a different script and that's enough for my achy brain for today.

 

i've done some ahk work with keystates, sending, opening and manipulating app windows, and that's about it. see, 8 posts, haven't done much w/ ahk yet. I used to make simple visual basic apps like 15 years ago and i could do some funny stuff to America Online back then, lol. also i used to make business reports with basic (not visual basic), cobol and rpg for 3 grueling hours a day for two years. we worked with variables, subroutines, and data tables and stuff. but THAT was a long time ago and even then the languages were on their way out. so when i looked up the commands i have learned about for ahk they seemed VERY similar to basic, anyways, i'm sure i have the potential to look each and every one of these up and understand how they're being used. I always have to change SOMETHING when someone gives me code and I have to understand it to do so.

 

Thanks so far though!



robert_ilbrink
  • Members
  • 561 posts
  • Last active: Mar 10 2015 08:36 PM
  • Joined: 05 May 2012

I investigated alternatives to getting the last line.

http://www.autohotke...gs-dll-version/

https://ahknet.autoh...lib.htm#TF_Tail

 

And I am still not sure this will be fast enough with 35 MB.

 

I wonder of we can monitor the chat box directly.

Could you try to see if you can detect any regular text in the chatbox with the use of AHK Windows Spy?

 

Good that you want to understand the code, that is why I tried to comment each important line.

 

Let's see how far we can get here.



robert_ilbrink
  • Members
  • 561 posts
  • Last active: Mar 10 2015 08:36 PM
  • Joined: 05 May 2012

You might even be able to read the memory location of the chat with this:

 

http://www.autohotke...emory-function/

 

and this:

 

http://www.cheatengine.org/



joelreitzloff
  • Members
  • 12 posts
  • Last active: Aug 07 2016 02:30 AM
  • Joined: 28 Dec 2007

[TO  : Piotr JIIIS Wojtowicz] what language did you make the program in?
[FROM: Piotr JIIIS Wojtowicz] c++ builder
[FROM: Piotr JIIIS Wojtowicz] if you want to make your own
[FROM: Piotr JIIIS Wojtowicz] just parse entropia logs
[FROM: Piotr JIIIS Wojtowicz] and make screens using FRAPS
[TO  : Piotr JIIIS Wojtowicz] yeah i use fraps
[TO  : Piotr JIIIS Wojtowicz] can it be done with autohotkey?
[FROM: Piotr JIIIS Wojtowicz] hm
[FROM: Piotr JIIIS Wojtowicz] you have to use timer i guess
[TO  : Piotr JIIIS Wojtowicz] start remembering how to program in basic ;p
[TO  : Piotr JIIIS Wojtowicz] think VB would work?
[FROM: Piotr JIIIS Wojtowicz] if you like that language
[FROM: Piotr JIIIS Wojtowicz] i would wrote it now in c#
[TO  : Piotr JIIIS Wojtowicz] yeah
[FROM: Piotr JIIIS Wojtowicz] but before i only knew c++ builder
[FROM: Piotr JIIIS Wojtowicz] vb or c#
[FROM: Piotr JIIIS Wojtowicz] depends which you like more
[FROM: Piotr JIIIS Wojtowicz] i started with c, c++ so i use c# now
[TO  : Piotr JIIIS Wojtowicz] what method; did you use to moniter the log file?
[FROM: Piotr JIIIS Wojtowicz] in short:
[FROM: Piotr JIIIS Wojtowicz] (if i remember well)
[FROM: Piotr JIIIS Wojtowicz] start; load file; remember how much lines there are there
[FROM: Piotr JIIIS Wojtowicz] 1 sec.
[FROM: Piotr JIIIS Wojtowicz] after i.e. 1 sec check if file has more lines than before, if yes then read those and check if your name is there
[FROM: Piotr JIIIS Wojtowicz] and remember new line counts, and loop
[FROM: Piotr JIIIS Wojtowicz] in short
[TO  : Piotr JIIIS Wojtowicz] andn EU keeps updating the file while the program has it open?
[FROM: Piotr JIIIS Wojtowicz] ya, i open it just for read
[TO  : Piotr JIIIS Wojtowicz] do you remember how fast you made the loop?
[FROM: Piotr JIIIS Wojtowicz] i think i missed only 1 screnn in 1-2thousand
[FROM: Piotr JIIIS Wojtowicz] hm
[TO  : Piotr JIIIS Wojtowicz] ah yeah.. maybe that&apos;s just a lag quirk
[FROM: Piotr JIIIS Wojtowicz] you should experiment, half sec i think should be enough
[FROM: Piotr JIIIS Wojtowicz] even maybe 1 sec
[TO  : Piotr JIIIS Wojtowicz] right cuz it&apos;s gonna check all new lines anyway
[FROM: Piotr JIIIS Wojtowicz] ya, not whole file
[TO  : Piotr JIIIS Wojtowicz] nice, this is helpful
[FROM: Piotr JIIIS Wojtowicz]
[FROM: Piotr JIIIS Wojtowicz] ok g2g afk now
[FROM: Piotr JIIIS Wojtowicz] gl with your screener
[TO  : Piotr JIIIS Wojtowicz] ok. well ill keep yousing yours for the time being cuz its nice
[FROM: Piotr JIIIS Wojtowicz] thx
[TO  : Piotr JIIIS Wojtowicz] if i dont alt tab it runs perfectly
[TO  : Piotr JIIIS Wojtowicz] i LOVE the adjuster for the delay for hofs and globals seperatly, such beautiful screenshots
[TO  : Piotr JIIIS Wojtowicz] bye
 



guest3456
  • Members
  • 1704 posts
  • Last active: Nov 19 2015 11:58 AM
  • Joined: 10 Mar 2011
see here for proper tail function:
http://www.autohotke...le/#entry380710

Guest10
  • Members
  • 1216 posts
  • Last active: Oct 30 2015 05:12 PM
  • Joined: 27 Oct 2012

i did the following by replacing Last with First and some other changes and now the script gives the First line! how do i get the nth line?

; http://www.autohotkey.com/board/topic/92407-help-with-a-few-lines-of-code-reading-last-line-of-txt-file-as-the-file-updates/

#SingleInstance Force
;#installKeybdHook
;#Persistent

AppName=ReadFirst

;Menu, Tray, Tip, %AppName%
;Menu, Tray, Icon , Shell32.dll, 14, 1
;TrayTip, %AppName%, Started, 1

FileRead, AllLines, C:\...\file.txt ; Read ALL lines into AllLines
StringGetPos, FirstLineNr, AllLines,`n, R1, ; Get position of first line
StringTrimRight, FirstLine, AllLines, %FirstLineNr% ; Remove everything after first line
StringReplace, FirstLine, FirstLine, `n ; Remove line break

MsgBox, %FirstLine%
ExitApp


robert_ilbrink
  • Members
  • 561 posts
  • Last active: Mar 10 2015 08:36 PM
  • Joined: 05 May 2012

guest3456, Since the file has to be monitored (i.e. read many times a second) AND is around 35 MB in size, the line read loop takes way too long, that is why I tried to read everything at once and find the last line through StringGetPos.



guest3456
  • Members
  • 1704 posts
  • Last active: Nov 19 2015 11:58 AM
  • Joined: 10 Mar 2011
no

did you even try the function?

the function returns the new lines, and the loop only reads those

robert_ilbrink
  • Members
  • 561 posts
  • Last active: Mar 10 2015 08:36 PM
  • Joined: 05 May 2012

Guest10, Just renaming the variables does not change the code behaviour.

 

Instead of :

 

StringGetPos, LastLineNr, AllLines,`n, R1, ; Get position of LAST line

 

 

Write:

 

StringGetPos, FirstLineNr, AllLines,`n, L4, ; Get position of 4th line

 

 

Since you now have All the lines except the first 3, you need to loop parse the fisrt line in memory, something like...

 

 

Loop, parse, FirstLine, `n, `r
      {
         line := A_LoopField
         Break
      }
 
 
 
A totally other way is to use:  StringSplit, Line, AllLines, `n
The result would be variables like Line1, Line2, Line3 etc,
 
 
FileRead, AllLines, C:\...\file.txt ; Read ALL lines into AllLines

StringSplit, Line, AllLines, `n

MsgBox, %Line1%

 

 

or to see each line:

 

Loop

{

   MsgBox, % Line%A_Index%

}

Return

 

To know how many lines:

MsgBox, %Line0%

 

 

Or the traditional loopread and break when A_Index > 4



robert_ilbrink
  • Members
  • 561 posts
  • Last active: Mar 10 2015 08:36 PM
  • Joined: 05 May 2012

guest3456, Sorry, I did not try the function, but I will NOW.

When I saw the Loop read line, I thought Oh no, this ain't working, not realizing that the loop readline only uses the tailed data.

Thank you for pointing this out!