Results 1 to 11 of 11

Thread: Help with reading text

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2008
    Posts
    32

    Question 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.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Help with reading text

    vb.net Code:
    1. 'Get all the lines from the file.
    2. Dim lines As String() = IO.File.ReadAllLines("file path here")
    3.  
    4. 'Discard all but the last four lines.
    5. lines = lines.Skip(lines.Length - 4).ToArray()
    6.  
    7. For Each line As String In lines
    8.     'Split the line on the pipes.
    9.     Dim fields As String() = line.Split(","c)
    10.  
    11.     'Get the User Name field.
    12.     Dim userNameField As String = fields.Single(Function(f) f.TrimStart().StartsWith("User Name"))
    13.  
    14.     'Split the field into name and value.
    15.     Dim fieldParts As String() = userNameField.Split(":"c)
    16.  
    17.     'Get the field value.
    18.     Dim userName As String = fieldParts(1).Trim()
    19.  
    20.     MessageBox.Show(userName, "User Name")
    21. Next
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Member
    Join Date
    Aug 2008
    Posts
    32

    Re: Help with reading text

    Thanks mate I'll implement it now. ILY

  4. #4

    Thread Starter
    Member
    Join Date
    Aug 2008
    Posts
    32

    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.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Member
    Join Date
    Aug 2008
    Posts
    32

    Re: Help with reading text

    Did try it, couldn't get it working. Had a few errors.

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Help with reading text

    By the way, to put the caret at the end of the text:
    vb.net Code:
    1. myTextBox.StartPosition = myTextBox.TextLength
    You may have to call ScrollToCaret afterwards too.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Help with reading text

    Quote Originally Posted by Chaori View Post
    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Member
    Join Date
    Aug 2008
    Posts
    32

    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"))

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  11. #11

    Thread Starter
    Member
    Join Date
    Aug 2008
    Posts
    32

    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
  •  



Click Here to Expand Forum to Full Width