Use TextMerge to export FoxPro data

You can use the Copy To command to export a table into a text file with various forms of delimiter between fields. The TextMerge commands let you create a more complex text file. The contents can be a mixture of plain text, fields and variables, and FoxPro functions.

The commands you need are:

Set Textmerge On

Tell FoxPro to evaluate all the text it sees between the double chevron delimiters << and >>. If necessary, these delimiters can be changed with the Set Delimiters To command.

Set Textmerge To <filename> Noshow

Direct the output to a file. The file will be created if it does not already exist. If the file does exist and Safety is On then you will be asked whether you want to overwrite it. The Noshow clause suppresses output to the screen and is useful if this command is being used in an application.

\xyz

Send the characters 'xyz' to the file. The characters are preceded by a carriage return/linefeed combination. There is no need for quotes around the characters. If you put quotes around the characters then the quotes will be output as well.

\\xyz

Send the characters 'xyz' to the file on the current line.

Set TextMerge To

Close the output file.

Set Textmerge Off

Stop evaluating expressions within << and >>.

<<foxpro expression>>

Evaluate the FoxPro expression. The expression can be the name of a field or memory variable or a FoxPro function. The example below shows how you can mix <<foxpro expression>> with plain text on the same line.

Text Merge Example

This page shows the code for a simple text merge operation.

Visual FoxPro offers other ways of exporting data to HTML. These are described on the HTML from FoxPro page.

Gotcha

There are two things to watch out for when using text merge and a third when using it to export HTML:

  • Everything on the line after a \ or \\, including any trailing spaces, will be output.
  • The carriage return/linefeed comes before the characters are output.
  • Remember that a \\ produces a new line of output in the text of the HTML file. It will not produce a new line on the web page. You never really need to use a \\ when generating HTML because browsers ignore new lines in the HTML source.

Scope

The SCAN command can take a scope clause if you need to produce a file based on a particular set of records. More details on the scope of Visual FoxPro commands here.