VFP COPY TO CSV: What Visual FoxPro's CSV Export Changes and When It Is Enough

How the Visual FoxPro COPY TO command exports tables to CSV or tab-delimited text, what it changes in your data, where it falls short, and when a custom export is the safer choice.

Visual FoxPro can write a table or cursor out as comma-separated text with a single COPY TO command. That convenience is the main reason people reach for it, but the output is a transformation of the data, not a copy of it. Field types, memo contents, quoting and character encoding can all change along the way. This review explains what the command does, what it alters, and how to judge whether the resulting file will hold up once it reaches a spreadsheet, a database loader or another person.

Note on evidence: no test files, product measurements or benchmark results were supplied for this article. The behavior described below reflects the documented syntax of the COPY TO command and widely known characteristics of the CSV format. Points that depend on your Visual FoxPro version, settings or data are flagged so you can confirm them with a small test export.

Is COPY TO CSV suitable for your export?

For a quick, one-off export of simple tables, it usually is. The main benefit is speed and zero extra code: one line produces a file with a header row that most spreadsheet and import tools can open.

The decisive tradeoff is control. COPY TO decides how each field is quoted, how dates and logical values are written, and which fields are included, and it gives you few options to change those decisions. If your data contains memo fields, embedded double quotes, line breaks inside text, or characters outside your code page, the built-in export can silently lose or corrupt information.

A bounded recommendation:

  • Use TYPE CSV when the table has short character, numeric, date and logical fields, the data is clean, and the file is for viewing or a tolerant importer.
  • Use DELIMITED WITH TAB when the destination expects tab-separated text or when commas commonly appear inside your values.
  • Write a custom export (a loop that builds each line and writes it with low-level file functions or STRTOFILE) when you need memo contents, strict quoting with escaped quotes, a specific date format, or UTF-8 output.
  • Keep the original DBF as the source of record; treat the CSV as a derived copy, not a backup.

What the command actually does

COPY TO reads records from the currently selected table or cursor and writes them to a new file in the format named by the TYPE clause. With CSV, the first line contains the field names and each following line is one record with values separated by commas. Character values are enclosed in double quotes.

The related DELIMITED type produces similar comma-separated output but without the header line. Its WITH clause changes the details: WITH TAB separates fields with tab characters, WITH BLANK separates them with spaces, WITH a character changes the character used to enclose text values, and WITH CHARACTER changes the field separator itself. The SDF type is different again: it writes fixed-width columns with no separators, which is not CSV at all.

* Export the whole table with a header row
USE customers
COPY TO customers.csv TYPE CSV

* Export selected fields for one country only
COPY TO us_customers.csv FIELDS cust_id, company, city TYPE CSV FOR country = "USA"

* Tab-separated output (no header row)
COPY TO customers.tsv DELIMITED WITH TAB

* Write the text in a specific code page
COPY TO customers_1252.csv TYPE CSV AS 1252
Option | Separator | Header row | Text values enclosed | Typical use
TYPE CSV | Comma | Yes, field names | Double quotes | General spreadsheet or import use
DELIMITED | Comma | No | Double quotes | Importers that supply their own column list
DELIMITED WITH TAB | Tab | No | Verify in your version | TSV-style destinations, data with many commas
DELIMITED WITH CHARACTER x | Character x | No | Double quotes | Semicolon or pipe-separated files
SDF | None, fixed width | No | No | Legacy fixed-format systems

Which records are written depends on the current environment as well as the command. A FOR or WHILE clause, a scope such as NEXT or REST, and an active SET FILTER all narrow the output. Records marked for deletion are included unless SET DELETED is ON, so check that setting before exporting tables that use deletion marks.

What changes between the table and the CSV file

A DBF stores typed fields; a CSV file stores only text. Everything that made a value a date, a number or a logical becomes a string, and the receiving application has to guess or be told what each column means. The specific changes to check are:

  • Memo and general fields: delimited and CSV exports are documented as not carrying these fields. Long notes or descriptions held in memo fields will be missing from the file.
  • Embedded double quotes: CSV conventions double a quote that appears inside a quoted value. COPY TO is widely reported not to do this, which can break the column structure for the reader. Test with a record that contains a quote character.
  • Line breaks inside text: a carriage return or line feed inside a character value splits one record across lines in the output and most importers will misread it.
  • Dates: the date text written may not match your SET DATE display or the format your destination expects. Export a known date and inspect the raw file.
  • Logical fields: these are written as letters rather than words or numbers, which a destination may not recognize as true and false.
  • Trailing spaces: fixed-width character fields carry padding in the table; confirm whether your output keeps or trims it, because padded values can fail matching or lookups later.
  • Field names: the header uses field names as stored, so free-table names limited to ten characters appear in that shortened form.

Character encoding is a separate issue. Visual FoxPro works with single-byte and double-byte code pages, not Unicode. The AS clause controls which code page the text is written in, but it does not produce UTF-8. Accented letters and symbols may display incorrectly in tools that assume UTF-8 unless you convert the file afterwards or build UTF-8 text yourself, for example with STRCONV, in a custom export.

Will the file stay readable and usable after sharing?

Because the result is plain text, it copies, emails and versions cleanly, and any text editor can open it. A screen reader can read a CSV file line by line in a text editor, and a spreadsheet presents it as a navigable grid. Those are properties of plain-text CSV in general; no specific application or assistive technology was tested for this article, so confirm behavior in the tools your readers actually use.

Usability problems tend to appear when the file is opened rather than when it is created:

  • Spreadsheet programs often convert values automatically on open: leading zeros in codes such as ZIP codes or account numbers can disappear, long digit strings can turn into rounded numbers, and date-like text can be reinterpreted. Importing the file with column types set to text avoids this, while double-clicking it usually does not.
  • Regional settings matter: in locales that use a comma as the decimal mark, some programs expect semicolons as separators, so a comma-separated file may load into a single column. DELIMITED WITH CHARACTER can produce a semicolon-separated file if that is what the destination needs.
  • Database and script importers vary in how strictly they follow CSV quoting rules. A file that opens visually in one program can still fail a stricter loader, so validate against the actual destination rather than assuming one successful opening applies everywhere.
  • Missing memo data and broken quoting are not visible from the header row; scan a sample of records, especially ones with long or unusual text.

A short pre-export checklist

  • Confirm SET DELETED and any active filter so the right records are exported.
  • List which fields are memo or general type and decide how that data will travel, if at all.
  • Search character fields for double quotes, commas and line breaks.
  • Export a handful of records first and open the raw file in a text editor before opening it in a spreadsheet.
  • Check dates, logical values and any fields with leading zeros in the destination tool.
  • Decide on the target code page or UTF-8 and convert if needed.
  • If any check fails, switch to a custom export that writes each line with the quoting and formatting you control.

Bottom line

COPY TO with TYPE CSV is a practical way to get simple Visual FoxPro data into a widely readable text file, and for clean tables it is often all you need. It preserves the data's meaning only when the table avoids memo fields, embedded quotes, multi-line text and characters outside the chosen code page. When those are present, or when a strict importer is the destination, a small custom export routine is the more reliable option. In every case, verify a sample of the output in the tool that will consume it.