You wrote the following code to convert from TXT to DBF.
Do you know how to modify your code in order to convert from CSV to DBF?
Regards,
George
- Code: Select all Expand view
#include 'fivewin.ch'
#include 'xbrowse.ch'
function Main()
local cBuf
local aData, aHeaders
cBuf := MemoRead( 'input.txt' )
// parse and make a multi dimentional array
if cBuf[ -1 ] == 26 // check and remove Ctrl-Z
cBuf := Left( cBuf, Len( cBuf ) - 1 )
endif
if Right( cBuf, 2 ) != CRLF
// pad with CRLF if needed, not to miss the last line
cBuf += CRLF
endif
cBuf := StrTran( cBuf, CRLF, Chr( 10 ) )
aData := hb_aTokens( cBuf, Chr(10) )
AEval( aData, { |c,i| aData[ i ] := hb_aTokens( c, ';' ) } )
aHeaders := aData[ 1 ]
aData := ADel( aData, 1 )
aSize( aData, Len( aData ) - 1 )
// Parsing is done
// now view the data quickly in xbrowse before writing in DBF
xBrowse( aData, ; // data to browse
"Parsed Data as Array", ; // title
.f., ; // no autosort
{ |oBrw| AEval( oBrw:aCols, { |o,i| o:cHeader := aHeaders[ i ] } ) } ;
) // codeblock above assigns header names
// Extending the program to Write DBF
WriteToDBF( 'INPUT.DBF', aData, aHeaders )
// Check the DBF
USE INPUT
XBrowse()
return nil
static function WriteToDBF( cDbf, aData, aHeaders )
local aStruct := {}
local n, nLen := Len( aData )
AEval( aHeaders, ;
{ |c| AAdd( aStruct, ;
{ Upper( Left( Trim( c ), 10 ) ), 'C', 2, 0 } );
} )
// desirable to remove embedded chars not acceptable in fieldnames
// and to check for duplicates
// that logic is not included here
for n := 1 to nLen
AEval( aData[ n ], ;
{ |c,i| c := Trim( c ), ;
aStruct[ i ][ 3 ] := Max( Len( c ), aStruct[ i ][ 3 ] ), ;
aData[ n ][ i ] := c ;
} )
next n
dbCreate( cDbf, aStruct )
USE ( cDbf ) NEW ALIAS OUT EXCLUSIVE
for n := 1 to nLen
OUT->( dbAppend() )
AEval( aData[ n ], { |c,i| OUT->( FieldPut( i, c ) ) } )
next
OUT->( dbCloseArea() )
return nil