|
-
July 24th, 2008, 04:51 PM
#1
Reading text file, Please can anyone help
I have a text file which contains several bit of data, one being the time(including seconds), and another bit containing a random 6 character/number string, for example:
AAAAAB 1:1:1
AAAAAC 1:1:1
AAAAAA 1:1:2
AAAAAB 1:1:2
AAAAAA 1:1:2
Note: other information is between these but these are the two which i intend to use.
What i wish to do is make an exe which constantly runs which detects the newest times which have being added to the text file, on this example 1:1:2, and then follow by adding how many there are into a display box on the exe. For example in the sequence above the text box would display 3, but next is what i think will be trickiest, on line 3 and 5 the first 6 character/numbers are the same, so i only wish for the text box to increase by 1. Can the above be acheived and can someone prabs give me an example bit of coding or as much as i need.
Thank you very much,
any questions, as this is a mouth full, please reply.
-
July 24th, 2008, 05:56 PM
#2
Re: Reading text file, Please can anyone help
What have you tried so far?
This should get you started. Split the file into lines, and work from there, using the same concepts.
It first counts the lines, and then double-spaces the file and displays it in a msgbox.
Code:
Option Explicit
Private Sub Form_Load()
Dim x As Integer, st As String
Dim ff As Integer
Dim strBuff As String
Dim str() As String
ff = FreeFile
Open App.Path & "\to do.txt" For Input As #ff
strBuff = Input(LOF(ff), ff)
Close #ff
' ----------------- two ways to skin a cat --------------
MsgBox "Lines = " & Len(strBuff) - Len(Replace(strBuff, vbCrLf, "x")) + 1
' -------------------------------------------------------
str() = Split(strBuff, vbCrLf)
MsgBox "There are " & UBound(str) + 1 & " lines in the file"
For x = 0 To UBound(str)
st = st & str(x) & vbCrLf & vbCrLf
Next x
MsgBox st
End Sub
-
July 24th, 2008, 06:16 PM
#3
Re: Reading text file, Please can anyone help
I think the tricky part of the OP is how do you stream a file in VB. He wants to listen for new writes to the file. Everything after that seems pretty straightforward to me, but I can't think of how to stream the file in VB without constantly opening and closing the file.
-
July 24th, 2008, 06:26 PM
#4
Re: Reading text file, Please can anyone help
It depends on where it's coming from. If it's being used, he can't read it.
If you can read it every minute, that might be fine.
-
July 24th, 2008, 06:35 PM
#5
Re: Reading text file, Please can anyone help
Exactly...he could include a timer in the app and have it periodically run some routines to see if the file has been changed...if it has, update the display. In the event of the file being locked, he could have it keep trying until the file is available to check for changes.
Otherwise isn't there a way to get the properties of the file and check for the last modified date? Not sure if that is time stamped...but that could be a way to tell if something has been written to the file.
-
July 24th, 2008, 06:44 PM
#6
Re: Reading text file, Please can anyone help
The file is constantly updating and i need a program to tell me how many lines of input appear every second, but if the same code appears twice in one second, then the second and subsequent appearance are ignored.
-
July 24th, 2008, 06:55 PM
#7
Re: Reading text file, Please can anyone help
Is the file constantly open?
-
July 24th, 2008, 06:59 PM
#8
Re: Reading text file, Please can anyone help
Yes i would like it to be constant.
-
July 24th, 2008, 07:32 PM
#9
Re: Reading text file, Please can anyone help
read post #4 again. then answer that, also.
-
July 24th, 2008, 09:10 PM
#10
Re: Reading text file, Please can anyone help
If you just want the newest lines and keep the file open then I am thinking you could open it for binary access shared and then check the file size in each cycle of the timer. If the size has increased read the new bytes by calculating the newfilesize-oldfilesize seek to the position of the oldfilesize and read new-old bytes then process accordingly.
-
July 24th, 2008, 09:14 PM
#11
Re: Reading text file, Please can anyone help
If you are using the Scripting.FileSystemObject, you can always take a copy of the file...run your tests on the copy and then recopy, test, recopy, test ad nauseum...only prob will be some delay from the origional file writing to the testing. I have tested copying a file that is open with the file system object and it worked.
Otherwise I don't think you can read from it...unless you can access the code that is writing the file and run the checks there (before the write)...but then, I'm guessin you wouldn't need to do this if you could just do that.
-
July 24th, 2008, 09:16 PM
#12
Re: Reading text file, Please can anyone help
DataMiser, That is so clever. Don't know if I'd have the know how to implement that...but nicely done. Sounds like it would work. Actually after reading that again...I think I could do it.
-
July 25th, 2008, 01:13 AM
#13
Re: Reading text file, Please can anyone help
fso can't open a file that has a lock for write access, as some old serial programs do. for instance, dos programs.
-
July 25th, 2008, 12:55 PM
#14
Re: Reading text file, Please can anyone help
ah, I used fso completely for my test, opening the file and copying it. That must have been why it was able to copy while the file was open.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|