# csv udf

## Recommended Posts

Here's a udf to handle csv files.

I'm releasing the latest version _CSVLib_V1.3.au3, with nearly 10 new functions, all functions have been thoroughly reviewed and tested. I found a minor bug in _CSVConvertLineBreak, and I'll fix it soon.

The examples do not work with version 1.3, but a single file will be made available in the next revision in mid february.

I'm cought up in the middle of an install kit that I was hired for, so I really can't commit myself earlier than that.

All versions prior to 1.3 and their respective examples will be removed in that revision.

I'd really appreciate any help testing these functions or comments or ideas.

IVAN

CSVExample.au3

_CSVLib.au3

_CSVLib_V1.0.au3

CSVExampleFile_V1.au3

_CSVLib_V1.1.au3

CSVGuiExample_V1.1.au3

_CSVLib_V1.3.au3

Edited by ivan

##### Share on other sites

Great coding. I tried to trip it up using some combinations of things that I remember tripping Excel up. The parts I tried worked perfectly. I'll be able to use this code immediately.

Thanks.

##### Share on other sites

;===========================================================================================

; Function List:

; _CSVGetRecords: Retreives the contents of a csv file to a 1 Dim Array.

; _CSVGetColumn: Retreives the column of a csv file to a 1 Dim Array.

; _CSVRecordToFields: Converts a given Record (csv line) to a 1 Dim array of Fields

; _CSVStringToField: Converts a text string to csv format to store in a single field.

; _CSVFieldToString: Converts the content of a single csv field to a text string.

; Author: IVAN PEREZ

;===========================================================================================

i dont see this function in the list above

;===========================================================================================

; Function Name: _CSVGetField

............

????

8)

##### Share on other sites

Thanks for the encouragement guys.

I've got about 10 changes to the previous scripts including functions that specifically convert delimiters and enclose characters, as well as writing csv. I'll make them available as soon as I get back to the office in early January.

@Valuater:

Thanks. I added _CSVGetField to the list and updated the original post.

However, those are likely to be changed as I'll be including CSV write functions as well as delimiter and enclose conversions. I'll also improve the example and provide some documentation on the processes used. It's an honor that you've taken a look at my script.

Regards and merry crhistmas.

IVAN

Edited by ivan

##### Share on other sites

be sure to check this one also...

;===========================================================================================
; Function Name:     _CSVFieldToString
; Description:        Converts the content of a single csv field to a text string.
; Syntax:           _CSVFieldToString($pRawField [,$pEnclose] [,$pDelimiter]) ; Parameter(s):$pRawField    -    Raw csv string.
;                    $pDelimiter - Use -1 for default field separator char ','. ;$pEnclose    -    Use -1 for default quote char '"'.
; Requirement(s):   None
; Return Value(s):  On Success    -    1 Dim $lRecords array ; On Failure - Errors not defined yet ; Warning: Not tested with Delimiters other than the defaults. ; Date: 20/12/06 ; Author: IVAN PEREZ ;=========================================================================================== Func _CSVFieldToString($pRawField, $pDelimiter,$pEnclose)
Local $lStr =$pRawField
; strip StartEnclose and EndEnclose
If StringLeft($lStr, 1) =$pEnclose And StringRight($lStr, 1) =$pEnclose Then
$lStr = StringRight($lStr, StringLen($lStr) - 1)$lStr = StringLeft($lStr, StringLen($lStr) - 1)
$lStr = StringReplace($lStr, $pEnclose &$pEnclose, $pEnclose) EndIf Return$lStr
EndFunc   ;==>_CSVFieldToString

where...

$pDelimiter - Use -1 for default field separator char ','. is not used within the function .... keep up the good work 8) #### Share this post ##### Link to post ##### Share on other sites New version of the csv udf available. See first post. Help needed for testing. I'm away and have little access to a pc with autoit and scite. Thanks IVAN Edited by ivan #### Share this post ##### Link to post ##### Share on other sites Thanks again Valuater. I've implemented your suggestion in _CSVLib_V1.0.au3. This modified script with about a dozen new functions ought to work... Some functions have not even been tested, but they rely on the same principle. I'd be really grateful if you or anyone else can give me a hand testing them, as I'm away and have little time to look at the script. IVAN be sure to check this one also... ;=========================================================================================== ; Function Name: _CSVFieldToString ; Description: Converts the content of a single csv field to a text string. ; Syntax: _CSVFieldToString($pRawField [,$pEnclose] [,$pDelimiter])
; Parameter(s):     $pRawField - Raw csv string. ;$pDelimiter    -    Use -1 for default field separator char ','.
;                    $pEnclose - Use -1 for default quote char '"'. ; Requirement(s): None ; Return Value(s): On Success - 1 Dim$lRecords array
;                    On Failure    -    Errors not defined yet
; Warning:            Not tested with Delimiters other than the defaults.
; Date:                20/12/06
; Author:            IVAN PEREZ
;===========================================================================================
Func _CSVFieldToString($pRawField,$pDelimiter, $pEnclose) Local$lStr = $pRawField ; strip StartEnclose and EndEnclose If StringLeft($lStr, 1) = $pEnclose And StringRight($lStr, 1) = $pEnclose Then$lStr = StringRight($lStr, StringLen($lStr) - 1)
$lStr = StringLeft($lStr, StringLen($lStr) - 1)$lStr = StringReplace($lStr,$pEnclose & $pEnclose,$pEnclose)
EndIf
Return $lStr EndFunc ;==>_CSVFieldToString where...$pDelimiter - Use -1 for default field separator char ','.

is not used within the function

.... keep up the good work

8)

##### Share on other sites

You cant just post... testing is needed

+> Starting AutoIt3Wrapper v.1.7.4

>Running AU3Check (1.54.5.0) params: from:C:\Program Files\AutoIt3\beta

C:\Program Files\AutoIt3\Examples\My Stuff\test1212_old1_old2_old1_old3.au3(582,92) : ERROR: _CSVFieldToString() called with wrong number of args.

$lFields[uBound($lFields) - 1]= _CSVFieldToString($lRecord[$j], $pDelimiter,$pEnclose)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^

C:\Program Files\AutoIt3\Examples\My Stuff\test1212_old1_old2_old1_old3.au3(520,51) : REF: definition of _CSVFieldToString().

Func _CSVFieldToString($pRawField,$pEnclose = -1)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^

C:\Program Files\AutoIt3\Examples\My Stuff\test1212_old1_old2_old1_old3.au3(596,64) : ERROR: _CSVFieldToString() called with wrong number of args.

$lField = _CSVFieldToString($pConvert, $pDelimiter,$pEnclose)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^

C:\Program Files\AutoIt3\Examples\My Stuff\test1212_old1_old2_old1_old3.au3(520,51) : REF: definition of _CSVFieldToString().

Func _CSVFieldToString($pRawField,$pEnclose = -1)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^

C:\Program Files\AutoIt3\Examples\My Stuff\test1212_old1_old2_old1_old3.au3(649,92) : ERROR: _CSVFieldToString() called with wrong number of args.

$lFields[uBound($lFields) - 1] = _CSVFieldToString($lRecord[$j], $pDelimiter,$pEnclose)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^

C:\Program Files\AutoIt3\Examples\My Stuff\test1212_old1_old2_old1_old3.au3(520,51) : REF: definition of _CSVFieldToString().

Func _CSVFieldToString($pRawField,$pEnclose = -1)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^

C:\Program Files\AutoIt3\Examples\My Stuff\test1212_old1_old2_old1_old3.au3(663,64) : ERROR: _CSVFieldToString() called with wrong number of args.

$lField = _CSVFieldToString($pConvert, $pDelimiter,$pEnclose)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^

C:\Program Files\AutoIt3\Examples\My Stuff\test1212_old1_old2_old1_old3.au3(520,51) : REF: definition of _CSVFieldToString().

Func _CSVFieldToString($pRawField,$pEnclose = -1)

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^

C:\Program Files\AutoIt3\Examples\My Stuff\test1212_old1_old2_old1_old3.au3 - 4 error(s), 0 warning(s)

!>AU3Check ended.rc:2

>Exit code: 0 Time: 0.518

8)

That was quick.

When I deleted the unecessary $pDelimiter in _CSVFieldToString, I forgot to feed the changes into the funcs that called it. It's taken care of and the correction has been uploaded in the original post. IVAN You cant just post... testing is needed 8) Edited by ivan #### Share this post ##### Link to post ##### Share on other sites Testing still needed. Some fixes conducted and example file provided. See original post Thanks for comments, suggestions or criticism to anyone interested. Edited by ivan #### Share this post ##### Link to post ##### Share on other sites Fixes on conversion functions in _CSVLib_V1.1 and new gui example in CSVGuiExample_V1.1 available in first post. New example should prove that any 2 chars can be used as delimiter and enclose respectively. Please provide feedback. IVAN Edited by ivan #### Share this post ##### Link to post ##### Share on other sites _CSVLib_V1.3.au3 is available. Major changes and corrections with respect to previous versions. Only minor bug in _CSVConvertLineBreak. IVAN #### Share this post ##### Link to post ##### Share on other sites Hi guys. I'll clean up in mid february the older versions as soon as i fix the bug in _CSVConvertLineBreak. I will only leave the current version and a couple of examples. } As noted in my previous posts, feedback would greatly be appreciated. IVAN #### Share this post ##### Link to post ##### Share on other sites I recently got a PM with a question about which version matches which example, so I apologize for the potential confusion. You should be able to tell from the include line on the example and download the library required. Version 1.3 does not have an associated example file, and there were significant changes to existing functions, not only to names, but the way they work, namely using ByRef in function parameters so as to speed up processes and reduce the amount of coding required to accomplish a task. I will make an example file for version 1.3 in mid february, as soon as I finish a job I'm doing, and there's a small bug in line the break conversion function which I need to fix. Once that is done, I WILL REMOVE ALL PREVIOUS VERSIONS AND LEAVE A SINGLE VERSION, BASED ON 1.3. 2 Reasons for this, 1) I'm running out of space 2) I don't want to create confusion Regards IVAN #### Share this post ##### Link to post ##### Share on other sites Looks nice... 1 Yes, an example would help and removal of the older versions is a must 2 I noticed an error return in an incorrect order (7) If Not _CSVColumnAppend($lRecords, $pColumn,$pDelimiter, $pEnclose,$pMode) Then
SetError(7, @error)*******************Here**
Return 0
EndIf
; open file in overwrite mode
$lCSVFileHandle = FileOpen($pFullPath, 2)
If $lCSVFileHandle = -1 Then FileClose($lCSVFileHandle)
SetError(5)
Return 0
EndIf
For $i = 1 To UBound($lRecords) - 1 Step 1
$lCheckWrite = FileWriteLine($lCSVFileHandle, $lRecords[$i])
If $lCheckWrite = 0 Then FileClose($lCSVFileHandle)
SetError(6)
Return 0
EndIf
If Not _CSVColumnAppend($lRecords,$pColumn, $pDelimiter,$pEnclose, \$pMode) Then
Return SetError(7, @error, 0)

Less code also!

..... Just some thoughts

I notice all the views and downloads but nobody seems to post any thoughts ( same with my threads )

lol

8)

Edited by Valuater

##### Share on other sites

@Valuater:

I'll certainly add the error setting/tracking as one of the top todos in my next revision in mid february.

Whenever I use @error in SetError it's mainly for back tracking purposes, so I'll strictly limit to that.

I guess that could make it easier to read.

Why didn't I think of Return SetError(7, @error, 0) ??? I think it's great!

I was also surprised to see that, except from the first reply and your greatly appreciated comments, no one else has posted anything. I believe that all the files are creating confusion, and now with the upgrades in version 1.3, confusion is likely to be even greater.

Hopefully there'll be more comments once people see it working in a more real example. I'm working on one that uses the driverquery command in cmd to output a csv file. Then I try to illustrate various functions with the driver query results. I still have some work to do on it, but so far it looks promising.

Thanks,

IVAN

##### Share on other sites

Sorry for the delay, and it's gonna take longer.

My daughter cought pneumonia over carnival and was hospitalized. She left the intensive care unit yesterday (over 4 days of traumatic experience for a 2 year old), so don't expect me around for a while.

IVAN

##### Share on other sites

Works like a charm for me. Thanks!

##### Share on other sites

Here's a udf to handle csv files.

I'm releasing the latest version _CSVLib_V1.3.au3, with nearly 10 new functions, all functions have been thoroughly reviewed and tested. I found a minor bug in _CSVConvertLineBreak, and I'll fix it soon.

The examples do not work with version 1.3, but a single file will be made available in the next revision in mid february.

I'm cought up in the middle of an install kit that I was hired for, so I really can't commit myself earlier than that.

All versions prior to 1.3 and their respective examples will be removed in that revision.

I'd really appreciate any help testing these functions or comments or ideas.

IVAN

Excellent coding.

##### Share on other sites

@John C.:

Thanks. Today's the first time I've had to llok at this post. I hope to have an upgrade implementing some suggestions and cleaning a bug in the line break conversion function in v1.3.

IVAN

## Create an account

Register a new account