Is there a way to export records from a foxpro table into a text file? I need each record to be a new line in the text file. The fields also need to be in the same order that they are in the record.
the strtofile() function worked. but i need to add a character return at the end of the string so each string is added to the text file on a new line. is there a way to do this?
jimoo has a good idea with STRTOFILE(). Use that if COPY TO ... DELIMITED WITH ... is not what you need.
I must admit that up to now I had been doing it the old way, as I also needed to format and display the data slightly different from what appears in the record itself:
[/code]* This example uses low-level file access
private nHandle, nReply, cReply
nHandle=FOPEN(in_filename,1)
SCAN && FOR cMyFilter
* FPUTS() adds a CR/LF {CHR(13)+CHR(10)}
* to the end of the written string
* Field 1 is C type
* Field 2 is N type
* Field 3 is D type
* Field 4 is L type
nReply=FPUTS(nHandle,UPPER(EVAL(FIELD(1)));
+STR(EVAL(FIELD(2),11,2));
+DTOS(EVAL(FIELD(3)));
+IIF(EVAL(FIELD(4)),"T","F")
ENDSCAN
IF FCLOSE(nHandle)
cReply="OK"
ENDIF[/code]
_FH = fcreate("mytxtfile.txt"
if _FH < 0 then
return
endif
select a
use yourtable
_NumberOfFields = fcount()
scan
*:Ciclo para formar la cadena con los datos de los campos del registro actual
_FieldString = ""
For _X = 1 TO _NumberOfFields
*:Obteniendo el nombre del campo segun el su orden en la tabla
_Campo = FIELD(_X)
*:Verificando el typo de dato que guardael campo
_VariableType = TYPE(FIELD(_X))
*:Convirtiendo cualquier tipo de dato que sea en caracter para poder unirlo con los demas
Do Case
Case _VariableType = [N]
_VariableChar = Alltrim(Str(&_Campo))
Case _VariableType = [D]
_VariableChar = DTOC(&_Campo)
Case _VariableType = [L]
_VariableChar = &_Campo
_VariableChar = IIF(_VariableChar,[.T.],[.F.])
OtherWise
_VariableChar = &_Campo
EndCase
*:Uniendo los datos del campo en una cadena de caracteres
_FieldString = _FieldString + IIF(_X=_NumberOfFields,_VariableChar,_VariableChar + [·])
EndFor
*:Escribiendo los datos del registro al archivo creado
=FPUTS(_FH,_FieldString)
ENDSCAN
FCLOSE(_fh)
MODI COMM MYTXTFILE.TXT
Return
use items
list to txt && output to a file txt.txt
To customize your reports you can use
set prin to txt.txt
set devi to print
go top
do while !eof()
@prow()+1 , 1 say "Item name : " + item_name
.
.
.
skip
enddo
set prin to
set devi to screen
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.