VBScript programs demonstrating how to read lines from a comma delimited (csv) file. VBScript functions are not good at reliably parsing values from such a file. Fortunately, Michael Harris, a Microsoft MVP (Most Valuable Professional), developed a VBScript solution that he shared in the newsgroups. The first program below is based on his work.

Comma delimited files are easy to read in Visual Basic. You use the Open command to open the file, the Input command to read one line at a time, and the EOF function to recognize the end of the file. The VB Input command has built in functionality to parse comma delimited files correctly. However, these commands are not available in VBScript. In VBScript you use the FileSystemObject to read the file as a TextStream object. The ReadLine method of the TextStream object does not parse the lines.

The first program demonstrates a function to convert each line read from the file into an array of field values. The function recognizes that field values are delimited by commas. Field values can be enclosed in quotes. Quotes and commas embedded in quoted strings are ignored. Any missing values in the csv file result in an empty value in the array.

ReadCSV.txt <<-- Click here to view or download the program

An alternative is to use ADO in a VBScript program to open the file with a Jet OLEDB driver that can parse comma delimited files. The second example demonstrates how to read such a file that does not have a header line.

ReadCSVFile2.txt <<-- Click here to view or download the program

The third example uses the Jet OLEDB driver to read a comma delimited file that has a header line. This allows the program to retrieve the field values by name.

ReadCSVFile3.txt <<-- Click here to view or download the program

Finally, an example that uses the JET OLEDB driver to read a comma delimited file that has a header line, then uses a Microsoft Access Jet driver to write records to an Access database.

ReadCSVFile4.txt <<-- Click here to view or download the program