Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Outputting results to a text file 1

Status
Not open for further replies.

randallJ

IS-IT--Management
Aug 15, 2000
90
GB
Hi

I have a program which evaluates a database and changes the value in a field if an expression is true. Everytime the expression is true I would like to send the record number and other information from various fields to a text file. What is the best way of doing this?
 
Create a cursor containing the record number and other required fields. Then COPY TO a text file. Something like this:

Code:
SELECT RECNO(), SomeField, AnotherField ;
  FROM MyTable ;
  INTO CURSOR csrTemp
SELECT csrTemp
COPY TO MyTextFile.TXT TYPE DELIMITED

The above is just to give you a general idea. The TYPE clause will need adjusting to suit the type of text file that you need.

Mike

__________________________________
Mike Lewis (Edinburgh, Scotland)

My Visual FoxPro site: www.ml-consult.co.uk
 
May be
Code:
lcVar = TRANSFORM(RECNO())+[;]+YourFld1+[;]+YourFld2 &&& ...
STRTOFILE(lcVar,PUTFILE("where to save"))


 
Zografski,
In case they are exporting numbers or logicals, shoould it be
lcVar = TRANSFORM(RECNO())+[;]+ transform(YourFld1)+[;]+transform(YourFld2) &&& ...
STRTOFILE(lcVar,PUTFILE("where to save"))

David W. Grewe Dave
 
Thank you all for your repies. David I have used your solution as it allows me to append to a text file after each record has been evaluated. However is there a way to force a carriage return after a line of text has been appended? Basically I am recording the details of those records that have been changed, but would like the details of each record to start on a new line.
 
oops sorry it was Zografski's solution.
 
Hi randallJ,

Code:
#define CRLF CHR(13)+CHR(10)
LOCAL lcVar
lcVar = []
SCAN
    ...
    lcVar = lcvar + TRANSFORM(RECNO())+[;]+YourFld1+[;]+YourFld2+CRLF
    ...
ENDSCAN
...
STRTOFILE(lcVar,PUTFILE("where to save"))
 
If the file you scan has many records to export the lcVar in the Sample by Zografski will over flow. May want to expand Z's idea with.

#define CRLF CHR(13)+CHR(10)
LOCAL lcVar, pcFile, lnHandle
lcVar = []
pcFile = "C:\MyDirectory\Myfilename"

IF FILE(pcFile)
lnHandle=FOPEN(pcFile,2)
ELSE
=FCREATE(pcFile)
lnHandle=FOPEN(pcFile,2)
ENDIF
IF lnHandle > 0
SCAN
...
lcVar = TRANSFORM(RECNO())+[;]+YourFld1+[;]+YourFld2+CRLF
=FPUTS(lnHandle,lcVar)
...
ENDSCAN
=FCLOSE(lnHandle)
ENDIF

David W. Grewe Dave
 
Other way ;~)
Code:
#define CRLF CHR(13)+CHR(10)
LOCAL lcVar
lcSaveFile = PUTFILE("where to save")
IF EMPTY(lcSaveFile)
   MESSAGEBOX("No File selected ?")
   RETURN
ENDIF

SCAN
    IF yourcondition = .t.
       lcVar = TRANSFORM(RECNO())+[;]+YourFld1+[;]+YourFld2+CRLF
       STRTOFILE(lcVar,lcSaveFile,1)
    ENDIF
ENDSCAN
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top