VBSCript to parse text file

Posted on 2006-03-29
Last Modified: 2008-10-03
I'm an old school C programmer and would like to make the switch to VBScript. I know how to do this in C but i really would like to do this using VBscript. I have a text file with the following contents:
                                               8403 SE May Valley Rd Issaquah WA  98027

The info in the lines vary but once i have the basic script i can modify it to suite my needs. I want open a text file, read it, parse it and then write the results to another txt file. I would like to seperate out the individual address fields. (i.e. Street address, city, state, zip). I want to reformat the line with commas separating the fields and write this to another file. In C i would parse these lines in reverse (using the space as a delimiter). On some of the lines there is an account #at the end of the lines. the account # is prepended by the string: "Acct #".

Any help would be most appreciated. I don't have the luxury of time to learn VBScript from scratch.

Question by:ronfast
    LVL 85

    Expert Comment

    by:Mike Tomlinson
    I can give you code to open a file and read it line by line.
    I can give you code to open a file and write to it line by line.

    *** But how the heck can you realiably parse a line like that using just spaces?

    How do you know where the address ends and the city begins?

    What if the city has a space in it?


        8403 SE May Valley Rd Colorado Springs CO  98027

    "Colorado Springs" is the city.  So how can you reliably know by just counting spaces when you have reached the end of the city?

    Author Comment

    you can't really but all i want is for it to work for the majority of the records (i.e. city with no space). This will eliminate 90% of my work. I can handle doing the last 10% by hand.
    LVL 85

    Accepted Solution

    So you just need basic info on how to read/write files with VBScript?

    Here is some bare bones code on how to read a file line by line and output lines to another file...

    Dim source, target
    source = "c:\source.txt"
    target = "c:\target.txt"

    Dim fso, f, f2
    Const ForReading = 1, ForWriting = 2

    Set fso = CreateObject("Scripting.FileSystemObject")
    If fso.FileExists(source) Then
       Dim inputLine, outputLine
       Set f = fso.OpenTextFile(source, ForReading, False)
       Set f2 = fso.OpenTextFile(target, ForWriting, True)
       While Not f.AtEndOfStream
          inputLine= f.ReadLine
          ' analyze inputLine in here using String manipulation Left(), Mid(), Right(), Split(), Instr() etc...
          ' build outputLine so it looks as desired...

          f2.WriteLine outputLine
       Set f2 = Nothing
       Set f = Nothing
       MsgBox "Done"
       MsgBox source, vbOKOnly, "Source File Not Found"
    End If
    Set fso = Nothing

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Suggested Solutions

    Here we come across an interesting topic of coding guidelines while designing automation test scripts. The scope of this article will not be limited to QTP but to an overall extent of using VB Scripting for automation projects. Introduction Now…
    Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
    In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now