Function for parsing a CSV file

Author Message
DiGiTAL.SkReAM

  • Total Posts : 1259
  • Scores: 7
  • Reward points : 0
  • Joined: 9/7/2005
  • Location: Clearwater, FL, USA
  • Status: offline
Function for parsing a CSV file Tuesday, January 30, 2007 1:35 AM (permalink)
0
I was looking all over for a function to do this, and what I found were a number of character-by-character routines that were slow as all get out. 
So, with ebgreen's help http://www.visualbasicscript.com/fb.aspx?m=42592 I was able to come up with a much faster solution.
This function allows me to parse through a CSV file of about 5megs in size, roughly 8,300 lines long, each line containing about 30 fields (a dump of our company's GAL from AD) in about 3.65 seconds on an Athlon 3000+. 
 
Enjoy.
 
 Function fParseCSV(sStringIn,sChar)
 'This function takes as input, a comma-delimited string, and returns the same string
 'with each "field" separated by "sChar".  This is useful when dealing with a CSV file that
 'only has SOME fields enclosed with quotes and others without quotes.  This will provide
 'a single method of separating fields for use in a Split() or other parsing in VBS.
  Dim sTest, aOutput, iParts, iStart, iStop, sElement, sFinalText
  sTest = Replace(sStringIn,",",sChar)
  sFinalText = sTest 
  iParts = 0
  aOutput = Array()
   Do While Len(sStringIn) > 0
    iStart = InStr(sTest, Chr(34))
     If iStart >= 0 Then
      iStop = InStr(iStart + 1, sTest, Chr(34))
       If iStop > 0 Then
        ReDim Preserve aOutput(iParts)
        aOutput(iParts) = Mid(sTest, iStart+1, iStop - iStart-1)
        iParts = iParts + 1
        sTest = Mid(sTest, iStop+1)
       Else
        Exit Do
       End If
     Else
      Exit Do
     End If
   Loop
   For Each sElement In aOutput
    sFinalText = Replace(sFinalText,sElement,Replace(sElement,sChar,","))
   Next 
  fParseCSV = Replace(sFinalText,Chr(34),"")
 End Function
 

 
"Would you like to touch my monkey?" - Dieter (Mike Meyers)

"It is better to die like a tiger, than to live like a pussy."
-Master Wong, from Balls of Fury
 
#1
    TNO

    • Total Posts : 2094
    • Scores: 36
    • Reward points : 0
    • Joined: 12/18/2004
    • Location: Earth
    • Status: offline
    RE: Function for parsing a CSV file Tuesday, January 30, 2007 4:03 AM (permalink)
    0
    I love non-COM script solutions, but FYI,
    for those of you who are SQL/ADO savvy, check this out: http://www.tek-tips.com/faqs.cfm?fid=3323

    Excellent work.
    <message edited by TNO on Tuesday, January 30, 2007 4:06 AM>
    To iterate is human, to recurse divine. -- L. Peter Deutsch
     
    #2
      DiGiTAL.SkReAM

      • Total Posts : 1259
      • Scores: 7
      • Reward points : 0
      • Joined: 9/7/2005
      • Location: Clearwater, FL, USA
      • Status: offline
      RE: Function for parsing a CSV file Tuesday, January 30, 2007 6:42 AM (permalink)
      0
      ADO is definately the way to go - if you can be certain that every machine your script will be running on has the correct configuration.
       
      "Would you like to touch my monkey?" - Dieter (Mike Meyers)

      "It is better to die like a tiger, than to live like a pussy."
      -Master Wong, from Balls of Fury
       
      #3

        Online Bookmarks Sharing: Share/Bookmark

        Jump to:

        Current active users

        There are 0 members and 1 guests.

        Icon Legend and Permission

        • New Messages
        • No New Messages
        • Hot Topic w/ New Messages
        • Hot Topic w/o New Messages
        • Locked w/ New Messages
        • Locked w/o New Messages
        • Read Message
        • Post New Thread
        • Reply to message
        • Post New Poll
        • Submit Vote
        • Post reward post
        • Delete my own posts
        • Delete my own threads
        • Rate post

        2000-2012 ASPPlayground.NET Forum Version 3.9