Page 1 of 1

Fieldname limit to 10 char. giving problem

Posted: Sun Oct 23, 2016 1:11 pm
by Marc Venken
Hello,

I convert csv files to dbf files by code.

I have the fieldnames in a array adata from a other piece of code, and want to make a dbf out of it.
The problem is that the fieldnames in csv are often longer than 10 characters.

adata contains example :

ID
Name
Price
information1
information2
information3
...

The routine below will do

ID
Name
Price
informatio
informatio
informatio

I need something like (2 digits to be save, so 8 from the fieldname and 2 incremental)

informat01
informat02
informat03

I'm working on Ascan and so, but so far no luck...

Code: Select all

function Builddbf(adata,Alengte,cDbf)
   LOCAL aStru := {}

//   msginfo(atostr(adata))

   for i = 1 to len(adata)
     cField = STRTRAN(aData[i], '"', '')
     aAdd( aStru, { cField , "C", alengte[i]+1 , 0 } )
   next
   dbCreate( cDbf , aStru )

return

 

Re: Fieldname limit to 10 char. giving problem

Posted: Sun Oct 23, 2016 4:25 pm
by Rick Lipkin
Marc

Question .. do you know the structure of the CSV file ? If so, you may be able to use the SDF append from option ..

Code: Select all

..  create your temp database.. the .dbf fields must be in the same order as the .csv 

Select 1
Append from yourfile.csv SDF delimited by ","
 
Not very elegant but it should work or you can use Ole and Ado to take an .Xls(x) to .Dbf .. lots of examples in this forum on how to import Excel.

Rick Lipkin

Re: Fieldname limit to 10 char. giving problem

Posted: Sun Oct 23, 2016 6:46 pm
by Marc Venken
Hey,

The append from is no option.

I read the csv files and change some values while importing. The csv's are never the same as the master data and there are many csv files to process trough time.

I only know some what of DBF's, so other struff like sql, .... are no option on this moment. (time limit) in order to learn the rest.

Re: Fieldname limit to 10 char. giving problem

Posted: Sun Oct 23, 2016 10:36 pm
by James Bott
Marc,

This is one reason why I suggested changing the header of the CSV file before it is imported into a DBF. If you change the header to use the standard fieldnames in the master DBF, then importing and processing it will be much easier.

Another possibility is to just read in the lines of the CSV file one at a time, then move the data to an array then process the array.

There are other possibilities too.

James

Re: Fieldname limit to 10 char. giving problem

Posted: Tue Oct 25, 2016 9:31 am
by AHF
James,

Is this what you need ?

Code: Select all


if len( cField ) > 10
   n++
   cField := substr( cField, 1, 10 - len ( alltrim( str( n, 0 ) ) ) ) + ;
                    padl( alltrim( str( n, 0) ), 2, "0" )
endif

 

Re: Fieldname limit to 10 char. giving problem

Posted: Tue Oct 25, 2016 3:04 pm
by aferra
LOCAL cFileCSV := "c:\dircvs\name.cvs"

oTxtFile := TTxtFile():New( cFileCSV, 0 )
oTxtFile:Skip() //skip the first line of the header

while !oTxtFile:lEoF()

?StrToken( oTxtFile:cLine, 1, ";" )
?StrToken( oTxtFile:cLine, 2, ";" )
?StrToken( oTxtFile:cLine, <n>, ";" )

oTxtFile:Skip()

ENDDO

oTxtFile:Close()

that's how I read the .csv files or any other

Re: Fieldname limit to 10 char. giving problem

Posted: Tue Oct 25, 2016 5:54 pm
by Marc Venken
AHF wrote:

Code: Select all


if len( cField ) > 10
   n++
   cField := substr( cField, 1, 10 - len ( alltrim( str( n, 0 ) ) ) ) + ;
                    padl( alltrim( str( n, 0) ), 2, "0" )
endif

 
Hey,

This was exactly what I needed. Thanks.

Marc

Re: Fieldname limit to 10 char. giving problem

Posted: Tue Oct 25, 2016 6:15 pm
by Marc Venken
aferra wrote:LOCAL cFileCSV := "c:\dircvs\name.cvs"

oTxtFile := TTxtFile():New( cFileCSV, 0 )
oTxtFile:Skip() //skip the first line of the header

while !oTxtFile:lEoF()

?StrToken( oTxtFile:cLine, 1, ";" )
?StrToken( oTxtFile:cLine, 2, ";" )
?StrToken( oTxtFile:cLine, <n>, ";" )

oTxtFile:Skip()

ENDDO

oTxtFile:Close()

that's how I read the .csv files or any other
This is interesting, and so much clearer and easy !!
A total other way as I do, but very nice.

Can you point me to an information page where this is more explaned ? I would like to look into this more.

Marc