Results 1 to 5 of 5

Thread: Get lines from a txt document

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2005
    Posts
    15

    Get lines from a txt document

    Say I have a text document called TEXT

    it's a *.txt file.

    if I wanted to grab all of the text on a specific line say...line 1, how would I do this with VB?

    I would want to put it to a string variable.

  2. #2
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,547

    Re: Get lines from a txt document

    Quote Originally Posted by Zlord
    Say I have a text document called TEXT

    it's a *.txt file.

    if I wanted to grab all of the text on a specific line say...line 1, how would I do this with VB?

    I would want to put it to a string variable.
    simple as pie ok lets say the file path is selected from cd1.filename heres the code

    VB Code:
    1. private sub command1_click
    2. dim temp as string
    3. dim filestuff() as string
    4. Fno = FreeFile
    5. Open cd1.FileName For Input As Fno
    6. temp = Input$(LOF(Fno), Fno)
    7. Close #Fno
    8. filestuff() = split(temp,vbnewline)
    9. temp = filestuff(0)
    10. End Sub

    this code opens the file puts it into temp splits it on everyline then temp becomes the first line or for lines u do stuff like filestuff(0 to how many lines)

    hope this helped

  3. #3

    Thread Starter
    New Member
    Join Date
    Oct 2005
    Posts
    15

    Re: Get lines from a txt document

    CD1.filename?

    No clue what you're talking about;

  4. #4
    PowerPoster Keithuk's Avatar
    Join Date
    Jan 2004
    Location
    Staffordshire, England
    Posts
    2,236

    Re: Get lines from a txt document

    VB Code:
    1. Dim FileIn As String
    2.  
    3. Open "C:\Text.txt" For Input As #1   ' Open file for input.
    4. Line Input #1, FileIn ' Read line of data.
    5. Text1.Text = FileIn
    6. Close #1   ' Close file.
    I don't see why you have to read all the file when you only need the first line. It saves using LOF and Split. It reads the first line from the file and puts this into Text1.Text.
    Keith

    I've been programming with VB for 25 years. Started with VB4 16bit Pro, VB5 Pro, VB6 Pro/Enterprise and now VB3 Pro. But I'm no expert, I'm still learning.

  5. #5
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,547

    Re: Get lines from a txt document

    Quote Originally Posted by Keithuk
    VB Code:
    1. Dim FileIn As String
    2.  
    3. Open "C:\Text.txt" For Input As #1   ' Open file for input.
    4. Line Input #1, FileIn ' Read line of data.
    5. Text1.Text = FileIn
    6. Close #1   ' Close file.
    I don't see why you have to read all the file when you only need the first line. It saves using LOF and Split. It reads the first line from the file and puts this into Text1.Text.
    um he was using line1 as an example but anyways cd1.filename is commondialog1 just the name is changed so less typing involved

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