Enrico Maria Giordano wrote:Can I see a reduced and self-contained sample showing the problem, please? If those commands are not working maybe they can be fixed.
EMG
#include "FiveWin.ch"
#include "FileIo.ch"
FUNCTION GenTxt(ndelimiter)
MsgRun('Sto generando file in csv. Aspetta un momento...', oApp():cAppName, ;
{ || DoTXT() } )
MsgInfo("Processo terminato."+CRLF+CRLF+"I file CSV generati sono stati salvati"+CRLF+CRLF+"nella cartella : "+oApp():cDocPath)
RETU NIL
FUNCTION DoTXT(ndelimiter)
LOCAL aFields
LOCAL cBuffer
LOCAL cDbfFile
LOCAL cTxtFile
LOCAL cValue
LOCAL cTable
LOCAL nHandle
LOCAL nFields
LOCAL nField
LOCAL nPos
local aCTipo := {"C","N","L","M","D"}
local aDTipo := {"Character","Numeric","Logical","Memo","Date"}
local aFiles := {}
local aDir := {}
local i
DEFAULT ndelimiter := 1 // , ; | // aDelimiters := {";",",","|"}
IF ! Empty(oApp():cDbfPath)
cDir := oApp():cDbfPath
aDir := Directory( cDir+"*.dbf")
else
MsgStop( i18n( "Esercizio non attivato correttamente." ) + CRLF + ;
i18N( "Per favore eseguire l'attivazione di un esercizio."))
DbCloseAll()
return nil
Endif
FOR i := 1 TO LEN( aDir )
aadd(aFiles, aDir[i,1])
NEXT
FOR i := 1 TO Len(aFiles)
cDbfFile := lower(aFiles[i])
cTxtFile := oApp():cDocPath +StrTran( cDbfFile, ".dbf", ".csv" )
IF FILE( cTxtFile )
DELETE FILE ( cTxtFile )
ENDIF
USE (oApp():cDbfPath+cDbfFile)
nHandle := fCreate( cTxtFile , FC_NORMAL )
nFields := fCount()
aFields := dbStruct()
cBuffer :=""
DO WHILE .NOT. Eof()
FOR nField := 1 TO nFields
//-------------------
// Beginning Record
//-------------------
DO CASE
CASE aFields[nField, 2] == "D"
cValue := Dtos( FieldGet( nField ))
CASE aFields[nField, 2] == "N"
cValue := Str( FieldGet( nField ))
CASE aFields[nField, 2] == "L"
cValue := If( FieldGet( nField ), "True", "False" )
OTHERWISE
cValue := '"'+Alltrim(FieldGet( nField )) +'"'
ENDCASE
//------------------
// Ending Record
//------------------
IF nField=nFields
cBuffer := Alltrim( cValue )+ CRLF
else
If ndelimiter=1
cBuffer := Alltrim( cValue )+","
elseif ndelimiter=2
cBuffer := Alltrim( cValue )+";"
elseif ndelimiter=3
cBuffer := Alltrim( cValue )+"|"
Endif
Endif
fWrite( nHandle, cBuffer )
NEXT nField
SKIP
ENDDO
* quit
dbCloseAll()
fClose( nHandle )
NEXT
RETURN NIL
function csvtransform()
LOCAL hCsv, cLine, aLine, nI := 0,aFiles[ADIR("*.csv")]
ADIR("*.csv", aFiles)
nFile = msglist(aFiles )
cFile = aFiles[nFile]
cDbf = STRTRAN(lower(cFile), ".csv", ".dbf")
// Calculate the max field lengt
// Caution : If there are CDX-files, dbf ceation will faile !! erase them first of code it...
IF ( hCsv := fOpen( cFile , 16 ) ) > 0
HB_FReadLine( hCsv, @cLine, chr( 10 ) )
aHeader = strtoarr(cLine)
nlenarray = len(aHeader)
aFieldcount = array(nLenarray)
afill(aFieldcount,1)
nTester = 1
nTeller = 1
cdeli = MsgSelect( { ";",","} )
WHILE HB_FReadLine( hCsv, @cLine, chr( 10 ) ) == 0
oWnd:SetMsg( "Process data "+str(nTeller++)) // Show progress, but slows down on large files
if cDeli = ","
FOR I := 1 TO nLenarray
if I < nLenarray
cStr = subStr( cLine, 1, at( [,], cLine ) - 1 )
cStr = STRTRAN(cStr, '"', '')
else
cStr = STRTRAN(cLine, '"', '')
endif
nlengte = len(alltrim(cStr))
if aFieldcount[ I ] < nLengte
afieldcount[i] = nLengte
endif
cLine := subStr( cLine, at( [,], cLine ) + 1 )
//cLine := subStr( cLine, at( ["], cLine ) + 1 )
NEXT
else
FOR I := 1 TO nLenarray
if I < nLenarray
cStr = subStr( cLine, 1, at( [;], cLine ) - 1 )
cStr = STRTRAN(cStr, '"', '')
else
cStr = STRTRAN(cLine, '"', '')
endif
nlengte = len(cStr)
if aFieldcount[ I ] < nLengte
afieldcount[i] = nLengte
endif
cLine := subStr( cLine, at( [;], cLine ) + 1 )
//cLine := subStr( cLine, at( ["], cLine ) + 1 )
NEXT
endif
ENDDO
builddbf(aHeader,aFieldcount,cDbf)
fClose( hCsv )
ELSE
alert( "BAD LUCK" )
return
ENDIF
fClose( hCsv )
close all
Marc Venken wrote:I use this : (found from the forum)
Maybe this be be updated of made better.
- Code: Select all Expand view RUN
function csvtransform()
LOCAL hCsv, cLine, aLine, nI := 0,aFiles[ADIR("*.csv")]
ADIR("*.csv", aFiles)
nFile = msglist(aFiles )
cFile = aFiles[nFile]
cDbf = STRTRAN(lower(cFile), ".csv", ".dbf")
// Calculate the max field lengt
// Caution : If there are CDX-files, dbf ceation will faile !! erase them first of code it...
IF ( hCsv := fOpen( cFile , 16 ) ) > 0
HB_FReadLine( hCsv, @cLine, chr( 10 ) )
aHeader = strtoarr(cLine)
nlenarray = len(aHeader)
aFieldcount = array(nLenarray)
afill(aFieldcount,1)
nTester = 1
nTeller = 1
cdeli = MsgSelect( { ";",","} )
WHILE HB_FReadLine( hCsv, @cLine, chr( 10 ) ) == 0
oWnd:SetMsg( "Process data "+str(nTeller++)) // Show progress, but slows down on large files
if cDeli = ","
FOR I := 1 TO nLenarray
if I < nLenarray
cStr = subStr( cLine, 1, at( [,], cLine ) - 1 )
cStr = STRTRAN(cStr, '"', '')
else
cStr = STRTRAN(cLine, '"', '')
endif
nlengte = len(alltrim(cStr))
if aFieldcount[ I ] < nLengte
afieldcount[i] = nLengte
endif
cLine := subStr( cLine, at( [,], cLine ) + 1 )
//cLine := subStr( cLine, at( ["], cLine ) + 1 )
NEXT
else
FOR I := 1 TO nLenarray
if I < nLenarray
cStr = subStr( cLine, 1, at( [;], cLine ) - 1 )
cStr = STRTRAN(cStr, '"', '')
else
cStr = STRTRAN(cLine, '"', '')
endif
nlengte = len(cStr)
if aFieldcount[ I ] < nLengte
afieldcount[i] = nLengte
endif
cLine := subStr( cLine, at( [;], cLine ) + 1 )
//cLine := subStr( cLine, at( ["], cLine ) + 1 )
NEXT
endif
ENDDO
builddbf(aHeader,aFieldcount,cDbf)
fClose( hCsv )
ELSE
alert( "BAD LUCK" )
return
ENDIF
fClose( hCsv )
close all
Silvio.Falconi wrote:Marc Venken wrote:I use this : (found from the forum)
Maybe this be be updated of made better.
Silvio :
where is the source code complete ?
Return to FiveWin for Harbour/xHarbour
Users browsing this forum: No registered users and 67 guests