Fieldname limit to 10 char. giving problem

Post Reply
User avatar
Marc Venken
Posts: 727
Joined: Tue Jun 14, 2016 7:51 am

Fieldname limit to 10 char. giving problem

Post 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

 
Marc Venken
Using: FWH 20.08 with Harbour
User avatar
Rick Lipkin
Posts: 2397
Joined: Fri Oct 07, 2005 1:50 pm
Location: Columbia, South Carolina USA

Re: Fieldname limit to 10 char. giving problem

Post 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
User avatar
Marc Venken
Posts: 727
Joined: Tue Jun 14, 2016 7:51 am

Re: Fieldname limit to 10 char. giving problem

Post 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.
Marc Venken
Using: FWH 20.08 with Harbour
User avatar
James Bott
Posts: 4654
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA
Contact:

Re: Fieldname limit to 10 char. giving problem

Post 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
FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10
AHF
Posts: 837
Joined: Fri Feb 10, 2006 12:14 pm

Re: Fieldname limit to 10 char. giving problem

Post 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

 
Regards
Antonio H Ferreira
aferra
Posts: 88
Joined: Wed Apr 28, 2010 6:34 pm

Re: Fieldname limit to 10 char. giving problem

Post 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
User avatar
Marc Venken
Posts: 727
Joined: Tue Jun 14, 2016 7:51 am

Re: Fieldname limit to 10 char. giving problem

Post 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
Marc Venken
Using: FWH 20.08 with Harbour
User avatar
Marc Venken
Posts: 727
Joined: Tue Jun 14, 2016 7:51 am

Re: Fieldname limit to 10 char. giving problem

Post 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
Marc Venken
Using: FWH 20.08 with Harbour
Post Reply