WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Arrays
plus
plus

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

ArrayFileGetCsv - How to detect invalid CSV line

 Keywords: ArrayFileGetCSV Error 1808 File Invalid CSV line Bad Format Formatting @LF Validate CSV

Question:

On using ArrayFileGetCSV an "Error 1808: File contains invalid CSV line(s)" can happen. Is there any chance to get the line number from the input file which has triggered the error message?

Answer:

;-----------------------------------------------------------------------------------------------------------------------------------------
#DefineFunction udfCheckCSVFile (strCSVFile, strDelimiter, blnTrim)
; Trim leading and trailing empty lines.
If !!blnTrim
   intBytesWritten = FilePut (strCSVFile, StrReplace (StrTrim (StrReplace (FileGet (strCSVFile), @CRLF, @TAB)), @TAB, @CRLF))
EndIf

blnRetVal = @TRUE
strFileTemp = FileCreateTemp ("WBT")

arrCSVFile = ArrayFileGet (strCSVFile)
intLines = ArrInfo (arrCSVFile, 1)
intLineLast = intLines - 1

IntControl (72, 1, @TRUE, "", 0) ; On next cancel event goto label :CANCEL.

For intLine = 0 To intLineLast
   intBytesWritten = FilePut (strFileTemp, arrCsvFile[intLine])

   intLastEM = ErrorMode (@OFF)
   LastError ()
   arrTest = ArrayFileGetCSV (strFileTemp, 0, strDelimiter)
   intLastError = LastError ()
   ErrorMode (intLastEM)
   If intLastError > 0
      blnRetVal = @FALSE
      Pause ("Error in CSV line", "Line: " : intLine + 1 : @LF : arrCsvFile[intLine])
   EndIf
Next

:CANCEL
Drop (arrTest, arrCSVFile)
blnresult = FileDelete (strFileTemp)

Return blnRetVal
;..........................................................................................................................................
; This UDF udfCheckCSVFile is a simple helper to check a given CSV file for possible formatting errors.
;
; The given CSV File must
; - have CRLF end of line delimiters;
; - not have any @TAB character;
; - allow read and write access when trimming leading and trailing empty lines.
;
; This UDF returns a boolean value @TRUE (1) or @FALSE (0) to indicate that the given file can be used as a CSV file or not.
;
; Detlev Dalitz.20100814.
;..........................................................................................................................................
#EndFunction
;------------------------------------------------------------------------------------------------------------------------------------------






Article ID:   W17655
Filename:   ArrayFileGetCsv - How to detect invalid CSV line.txt
File Created: 2014:04:18:08:41:08
Last Updated: 2014:04:18:08:41:08