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"

Split

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.

Usage

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
MyCSVFile.OpenFile("demo.csv")

' 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) & ", ")
    Next
    Response.Write("<br>")

Wend

' Clean up
MyCSVFile.Close
%>

Properties

ErrorText
String. Error Description if a method reported False.

VirtualFileName
String. Contains the virtual path and file name.

AbsoluteFileName
String. Contains the physical path and file name.

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

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

Delimiter
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.

FieldWidths
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.

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

Fields
Array. Array of fields read by ReadLine method.

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

Methods

OpenFile
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

ReadLine
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

WriteLine
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

SplitDelimiter
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.

SplitFixed
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.

CloseFile
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
http://www.beanland.net.au/programming/

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.