'********************************************************************
'*
'* Function CSVSplit
'*
'* Author: NetworkAdminKB.com
'* Created: 2007-07-11
'* Modified: 2007-07-11
'*
'* Purpose: Returns an array containing the string data separated by the
'* specified delimiter similar to the VBScript Split function.
'* However, this function accounts for quotes in the string,
'* just in case the data has a delimiter in the text that is not
'* acting as a separator.
'*
'* Input: strCSV A deliminated string.
'* strDelimiter The delimiter for the variables in the line.
'* Default = ","
'*
'* Output: Returns an array of string data separated by the specified
'* delimiter.
'* Text qualifiers and delimeters are stripped from records before
'* being added to the array.
'* If no delimeters are found in strCSV then strCSV is returned in
'* a single item array.
'*
'* Notes: A string consists of all characters between two Delimiters (eg: ,)
'* or all characters between ," and ",.
'* Spaces after commas are considered part of the text and are not
'* stripped.
'* This is a modified version of the CSVParse Function
'*
'********************************************************************
Function CSVSplit(ByVal strCSV, ByVal strDelimiter)
'Version: 1.0 2007-07-11
Dim i, aryComma, aryResult, intQpos1, intQpos2, intLen
Dim strTextQualifier
strTextQualifier = Chr(34) ' <- Chr(34) is the ascii code for "
i = 0
aryComma = Array(i)
aryResult = Array(i)
'Set default if not specified
If Len(strDelimiter) = 0 Then strDelimiter = ","
aryComma(i) = InStr(1, strCSV, strDelimiter)
'If no comma found in string return the original string as single item array.
If aryComma(i) = 0 Then
'Wscript.Echo "no comma"
CSVSplit = Array(strCSV)
Exit Function
Else
'Comma found in string
aryResult(i) = Left(strCSV, aryComma(i)-1)
If InStr(1, aryResult(i), strTextQualifier) <> 0 Then
intQpos1 = InStr(1, strCSV, strTextQualifier)
intQpos2 = InStr(intQpos1 +1 , strCSV, strTextQualifier)
aryComma(i) = InStr(intQpos2 +1, strCSV, strDelimiter)
aryResult(i) = Mid(strCSV, intQpos1+1, intQpos2-intQpos1-1)
End If 'InStr(1, aryResult(i), strTextQualifier) <> 0
Do While aryComma(i) <> 0
i = i + 1
ReDim Preserve aryComma(i)
ReDim Preserve aryResult(i)
aryComma(i) = InStr(aryComma(i-1)+1, strCSV, strDelimiter)
If aryComma(i) <> 0 Then
aryResult(i) = Mid(strCSV, aryComma(i-1)+1, _
aryComma(i) - aryComma(i-1) -1 )
If InStr(1, aryResult(i), strTextQualifier) <> 0 Then
intQpos1 = InStr(aryComma(i-1), strCSV, strTextQualifier)
intQpos2 = InStr(intQpos1 +1 , strCSV, strTextQualifier)
aryComma(i) = InStr(intQpos2 +1, strCSV, strDelimiter)
aryResult(i) = Mid(strCSV, intQpos1+1, intQpos2-intQpos1-1)
End If 'InStr(1, aryResult(i), strTextQualifier) <> 0
End If 'While aryComma(i) <> 0
Loop 'While aryComma(i) <> 0
intLen = Len(strCSV)
aryResult(i) = Right(strCSV, intLen - aryComma(i-1))
If InStr(1, aryResult(i), strTextQualifier) <> 0 Then
aryResult(i) = Replace(aryResult(i), strTextQualifier, "")
End If 'InStr(1, aryResult(i), strTextQualifier) <> 0
End If 'aryComma(i) = 0
CSVSplit = aryResult
End Function 'CSVSplit