[Last Call] Learn how to a build a cloud-first strategyRegister Now


VBSCript to parse text file

Posted on 2006-03-29
Medium Priority
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
  • 2
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16328143
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

ID: 16328167
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 86

Accepted Solution

Mike Tomlinson earned 2000 total points
ID: 16328200
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this post we will learn different types of Android Layout and some basics of an Android App.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Introduction to Processes

829 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