|
-
Sep 6th, 2010, 10:31 PM
#1
Thread Starter
Member
Help with reading text
Hi guys,
As the title says, I'm having some trouble with reading text and other stuff, I'll try to explain the situation as best I can.
Text File Layout:
Code:
Computer Name: COMPUTERNAM | User Name: TEST123 | Date: DD/MM/YYYY | Time: H:MM:SS AM
Okay, say you have 20 rows of the above, all with different Usernames, Times and Dates (The Computer Name field is the same for each row).
What I want this program to do is read the last 4 lines, so in this situation it would be rows 17, 18, 19 and 20. Then I need it to search each of those 4 rows for "| User Name: " and display the following 7 characters. (Username is always 7 characters.)
What I have so far:
So far I have been able to get it to open the file and display the whole text (the easiest part). The laptop name is derived from the user input.
Code:
Dim LaptopName As String = "\\path_to_shared_network_drive\" & txtInput & ".txt"
Dim TextLine As String
Dim objReader As New System.IO.StreamReader(LaptopName)
Do While objReader.Peek() <> -1
UsedBy = TextLine & objReader.ReadLine() & vbNewLine
Loop
Using the above code I am only able to retrieve the last line of the text file.
Code:
objReader.Close()
UsedBy1 = Mid$(UsedBy1, 44, 7)
imgUsedBy1.Image = Image.FromFile("\\path_to_image_drive\images\" & UsedBy1 & ".jpg"
This code works to an extent. The issue with this part is, the computer name length differs from the type of laptop, so starting at position 44 will only work for 1 kind of computer. What I need instead of starting at 44 is to make it search for " | User Name: " and displaying the following 7 characters (The same method would also be used for TIME and DATE).
If anyone could help me sort this out I'd really appreciate it. Please ask if you need any more info or anything is unclear.
-
Sep 6th, 2010, 11:30 PM
#2
Re: Help with reading text
vb.net Code:
'Get all the lines from the file. Dim lines As String() = IO.File.ReadAllLines("file path here") 'Discard all but the last four lines. lines = lines.Skip(lines.Length - 4).ToArray() For Each line As String In lines 'Split the line on the pipes. Dim fields As String() = line.Split(","c) 'Get the User Name field. Dim userNameField As String = fields.Single(Function(f) f.TrimStart().StartsWith("User Name")) 'Split the field into name and value. Dim fieldParts As String() = userNameField.Split(":"c) 'Get the field value. Dim userName As String = fieldParts(1).Trim() MessageBox.Show(userName, "User Name") Next
-
Sep 7th, 2010, 12:05 AM
#3
Thread Starter
Member
Re: Help with reading text
Thanks mate I'll implement it now. ILY
-
Sep 7th, 2010, 12:11 AM
#4
Thread Starter
Member
Re: Help with reading text
Another quick question, don't suppose you know how to make the text box automatically scroll to the bottom when it opens the text document?
EDIT: On another thought, I don't think the above code will work. I need each of the last 4 rows seperated into 4 different variables so I can extract data from them, not all 4 rows as one chunk of text.
Thanks again.
Last edited by Chaori; Sep 7th, 2010 at 12:25 AM.
-
Sep 7th, 2010, 12:52 AM
#5
Re: Help with reading text
You should try the code rather than assume that it won't work. File.ReadAllLines returns a String array, so you DO have individual lines.
-
Sep 7th, 2010, 12:52 AM
#6
Thread Starter
Member
Re: Help with reading text
Did try it, couldn't get it working. Had a few errors.
-
Sep 7th, 2010, 12:53 AM
#7
Re: Help with reading text
By the way, to put the caret at the end of the text:
vb.net Code:
myTextBox.StartPosition = myTextBox.TextLength
You may have to call ScrollToCaret afterwards too.
-
Sep 7th, 2010, 12:55 AM
#8
Re: Help with reading text
 Originally Posted by Chaori
Did try it, couldn't get it working. Had a few errors.
I typed that code without an IDE so it might not be perfect. If you don;t tell us what the errors are, or even that there were errors, we won't be able to help you fix them. Note that that code uses LINQ, so it requires .NET 3.5 too.
-
Sep 7th, 2010, 01:04 AM
#9
Thread Starter
Member
Re: Help with reading text
"Sequence contains no matching element" on the line:
Dim userNameField As String = fields.Single(Function(f) f.TrimStart().StartsWith("User Name"))
-
Sep 7th, 2010, 01:30 AM
#10
Re: Help with reading text
In that case you have at least one record that doesn't have a field named "User Name" in it. I suggest that you check your data file. You can also have a look in the debugger at exactly what 'fields' does contain at that point. The code I wrote should handle the data as you described it. If the data is not as you described then you'll need to adjust the code accordingly.
-
Sep 7th, 2010, 02:37 AM
#11
Thread Starter
Member
Re: Help with reading text
Ahh yeah, there is a blank row at the end of every text file, that'd be why.
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
|