Results 1 to 2 of 2

Thread: Read Text File

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2007
    Posts
    26

    Read Text File

    Good Day Everyone,

    I need a little help with coding a loop.

    I have a page that read a txt file, the file looks like this:

    HDR#############SOME TEXT GOES HERE############
    DTL#############SOME TEXT GOES HERE############
    DTL#############SOME TEXT GOES HERE############
    DTL#############SOME TEXT GOES HERE############
    HDR#############SOME TEXT GOES HERE############
    DTL#############SOME TEXT GOES HERE############
    HDR#############SOME TEXT GOES HERE############
    DTL#############SOME TEXT GOES HERE############
    DTL#############SOME TEXT GOES HERE############

    The initial code was made to read only for 1 Header ans as many details as the text file had, but now the file contains variable quantity of HDRs and DTLs.

    How can i code a loop and output as separate readings for every HDR,

    Ex.

    HDR #1
    DTL
    DTL
    DTL

    HDR #2
    DTL

    HDR#3
    DTL
    DTL

    This it's to later update a MS Acces table.

    Thanks in advance for your help.

    Leo

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Read Text File

    vb Code:
    1. Set FS = CreateObject("Scripting.FileSystemObject")
    2.     Set f1 = fs.OpenTextFile("C:\Documents and Settings\peter\My Documents\basic\vbs\myhdrfile.txt", 1, False)
    3. mystr = f1.ReadAll
    4. f1.close
    5.  
    6. wscript.echo mystr
    7.  
    8. myarr = split(mystr, "HDR")
    9.  
    10. for i = 1 to ubound(myarr)
    11.     msgbox myarr(i)
    12. next
    this will add all the file into an array, each element starts with the HDR number followed by the data
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

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