I have a need to read a CSV file, and the only language I can use is VBscript.
I’m currently just opening the file and splitting on commas, and it’s working OK because there aren’t any quoted commas in fields. But I’m aware this is an incredibly fragile solution.
So, is there such a thing as a VBscript module I can use? Somewhere to get a tried-and-tested regular expression that would only split on commas not in quotes?
Any suggestions gratefully received.
VBScript does not have a module system comparable to Perl. However you can open CSV files with ADO and access them like a database table. The code would go something like this:
(The funny comments are solely to fix SO’s broken VB syntax highlighting)
Creating a
schema.inifile that exactly describes your input is optimal. If you don’t, you force the text driver to guess, and all bets are off if it guesses the wrong thing. Theschema.inimust reside in the same directory where your data is.Mine looked like this:
and with this
data.txt:I get this output:
Worth a read in this regard: Much ADO About Text Files off the MSDN.