TextFiler v1.21

TextFiler is an easy to use class which can read and write any Single byte or Unicode delimited files (CSV, Tab etc) and Fixed Width files. Full Microsoft Excel delimited rules are also adhered to:

The VBScript function Split does not handle the CSV (Comma Separated Values) format correctly. There is more to CSV files than simply being comma separated or delimited. This class contains a function which works just like Split, except that it applies extra (standard) rules above.

If you are only using Mixed Byte (UTF-8) files, then use my UTF8Filer class. It includes TextFiler's functionality and UTF-8 file handling.

For example, take this line from a csv file (generated from MS Excel or any other program). See how the different functions interpret the line differently:

Original lines from csv file

LNG,"Language Code",123,"Text, and Text","and ""this""","first line
second line of same field"


LNG "Language Code" 123 "Text and Text" "and ""this""" "first line

TextFiler's Split Delimiter

LNG Language Code 123 Text, and Text and "this" first line
second line of same field

See TextFilerDemo.asp for a full demo.


A simplistic version of your ASP could look something like this:

<!-- #include file=TextFiler.asp -->

' Initialise and open the file
Dim MyCSVFile, ArrayIndex
Set MyCSVFile = New TextFiler

' Read each line of a CSV file
While MyCSVFile.ReadLine
    'Display each field of the line
    For ArrayIndex = 0 to ubound(myLocalFields,1)

    Response.Write(myLocalFields(ArrayIndex) & ", ")


' Clean up


String. Error Description if a method reported False.

String. Contains the virtual path and file name.

String. Contains the physical path and file name.

Integer. 0 = ANSI (default), -1 = Unicode (standard TextStream format tristate constants)

Integer. 1 = Read (default), 2 = Write, 8 = Append (standard TextStream iomode constants)

Character. Only applicable to delimited files. , = Comma (default), vbTab = Tab, etc
Setting this property will instruct the class to run in Delimited  mode. Set FieldWidths to swap to Fixed Width mode.

String. Only applicable to Fixed Width files. Widths are in characters (not bytes) and are comma separated. ie "10,5,20,8"
When reading this property, it returns the widths converted to Integers in an array.
Setting this property will instruct the class to run in Fixed Width mode. Set Delimiter to swap to Delimited mode.

String. vbCRLF = carriage return & line feed (default), vbLF = Line feed, etc

Array. Array of fields read by ReadLine method.

Integer. Number of lines read from / written to the current file so far.


Returns: True if the file opened successfully
Parameters: Absolute or Virtual path and file name. Must not be relative (start with "../")
Syntax: OpenFile(FileName)
Example: if not OpenFile("myfile.csv") then 'do error handling
Opens a file for reading / writing

Returns: True if the line was read successfully. False if error or EOF
Parameters: none
Syntax: ReadLine
Example: while ReadLine
Reads fields from a file

Returns: True if the line was written successfully
Parameters: Array of fields
Syntax: WriteLine(myArray)
Example: if not WriteLine(myArray) then 'do error handling
Writes fields to a file

Returns: populates Fields property
Parameters: String of field data
Syntax: SplitDelimiter(LineString)
Example: SplitDelimiter("1243,abcd,4321,dcba")
Converts a string to an array. This is not normally used directly, but is exposed for you to use if you have the need.

Returns: populates Fields property
Parameters: String of field data. Reads FieldWidths property
Syntax: SplitFixed(LineString)
Example: SplitFixed("1243abcd   4321           dcba")
Converts a string to an array. This is not normally used directly, but is exposed for you to use if you have the need.

Returns: nothing
Parameters: none
Syntax: CloseFile
Example: CloseFile
Closes the file, flushing the ASP file buffer. Releases resources.

Important Notes

Run TextFilerDemo.asp for a full demo and reference code.

If you improve this code, please send me a copy! Thanks!

Hunter Beanland
hunter @ beanland.net.au

Version History
1.2 Changed to Class format, added fixed width functions

1.1 Support in the example to handle rows which span more than one line
1.0 First version.