Jump to content


Photo

Constantly read a perpetually written to txt file


  • Please log in to reply
5 replies to this topic

#1 fenton

fenton
  • Members
  • 62 posts

Posted 06 November 2010 - 03:20 AM

We use terminal emulation to communicate to an AIX server running database software. I was wondering what the best way to capture, evaluate, and perform action on text coming to the screen would be.
The emulation software (AnzioLite) has a capture function, which constantly writes the screen output to a text file.
I was thinking about a file-reading loop, but the longer the window is open, the larger the file gets. I would only want text that was written to that file from the last page or two.
I'm sorry, I know this isn't much info to go on, so I'll come back and elaborate after sushi!
I just wanted to put this out there- is there some way to constantly read the text that is getting sent back and forth from the terminal and the server?

#2 Guests

  • Guests

Posted 06 November 2010 - 03:58 AM

You can probably use something like WatchFolder() (it should work when a file in a directory is changed).

Then use FileReadLine and keep track of last line read.

A little sloppy but it might work.

#3 Leef_me

Leef_me
  • Moderators
  • 7704 posts

Posted 06 November 2010 - 07:17 AM

Do some tests to see if the file is continuously locked.
If it isn't always locked, you could rename the file every-so-often.

In the process, you end up with a small file to read.

#4 fenton

fenton
  • Members
  • 62 posts

Posted 09 November 2010 - 12:05 AM

You can probably use something like WatchFolder() (it should work when a file in a directory is changed).Then use FileReadLine and keep track of last line read.

Thanks, I'll check that out.

Do some tests to see if the file is continuously locked.
If it isn't always locked, you could rename the file every-so-often. In the process, you end up with a small file to read.

Good idea- I tried it, but windows does say the file is locked.

#5 jethrow

jethrow
  • Fellows
  • 2549 posts

Posted 09 November 2010 - 01:45 AM

Constantly read a perpetually written to txt file

So, are you only wanting to read the newly written data? If you are using AutoHotkey_L, you could try this:
File := FileOpen("File.txt", "r")
File.pos := File.length
Loop,
	if (File.pos < File.length)
		TrayTip, Written:, % File.Read()
	else
		sleep, 100	
Esc::
	File.close()
	ExitApp
After this script starts, it will read any new data that is written to the file. However, if the other app locks the file, this might not work...

#6 guest3456

guest3456
  • Guests

Posted 18 November 2010 - 05:06 AM

function FileTail
originally from: http://de.autohotkey.../topic4619.html
looks like its based off: http://www.autohotke...opic.php?t=7549


f3::
   newtext := FileTail("C:\Program Files\mytextfile.txt")
   if (newtext) {
      Loop, parse, newtext, `n, `r
      {
         line := A_LoopField
         linenum := A_Index
         ;..
      }
   }
return


FileTail(File) {
   Static OPEN_EXISTING := 3
   Static GENERIC_READ := 0x80000000
   Static FILE_SHARE_READ := 1
   Static FILE_SHARE_WRITE := 2
   Static FILE_SHARE_DELETE := 4
   Static FILE_BEGIN := 0
   Static INVALID_HANDLE_VALUE := -1
   Static CF := ""   ; Aktuelle Datei (Current File)
   Static FP := 0    ; Aktuelle Position in der Datei (File Pointer)
   FH := 0           ; Dateihandle (File Handle)
   FS := 0           ; Dateigröße (File Size)
   FC := ""          ; Inhalt der gelesenen Zeilen (File Content)
   CL := 0           ; Länge des gelesenen Zeilen (Content Length)
   If (File != CF) {
      CF := File, FP := 0
   }
   FH := DllCall("CreateFile"
               , "Str",  File
               , "UInt", GENERIC_READ
               , "UInt", FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE
               , "UInt", 0
               , "UInt", OPEN_EXISTING
               , "UInt", 0
               , "UInt", 0)
   If (FH = INVALID_HANDLE_VALUE) {
      CF := "", FP := 0
      MsgBox, 262160, FileTail, Die Datei %File% konnte nicht geöffnet werden!
   } Else {
      DllCall("GetFileSizeEx"
            , "UInt",   FH
            , "Int64P", FS)
      If (FP = 0 Or FS <= FP) {
         FP := FS
      } Else {
         DllCall("SetFilePointerEx"
               , "UInt",  FH
               , "Int64", FP
               , "UInt",  0
               , "UInt",  FILE_BEGIN)
         CL := VarSetCapacity(FC, FS - FP, 0)
         DllCall("ReadFile"
               , "UInt",  FH
               , "UInt",  &FC
               , "UInt",  CL
               , "UIntP", CL
               , "UInt",  0)
         DllCall("CloseHandle", "UInt", FH)
         VarSetCapacity(FC, -1)
         FP := FS
      }
   }
   Return FC
}