Append From, Copy To, Import, Export
These commands let you move data in and out of DBF format. APPEND FROM and COPY TO also let you move data between tables.
Usage |
APPEND FROM FileName | ? [ FIELDS FieldList ] [ FOR lCondition ] [ [ TYPE ] [ DELIMITED [ WITH Delimiter | WITH BLANK | WITH TAB [ WITH CHARACTER Separator ] | DIF | FW2 | MOD | PDOX | RPD | SDF | SYLK | WK1 | WK3 | WKS | WR1 | WRK | CSV | XLS | XL5 [ SHEET SheetName ] | XL8 [ SHEET SheetName ] ] ] [ AS nCodePage ] IMPORT FROM FileName [ DATABASE DBCName [ NAME LongTableName ] ] [ TYPE ] FW2 | MOD | PDOX | RPD | WK1 | WK3 | WKS | WR1 | WRK | XLS | XL5 [ SHEET cSheetName ] | XL8 [ SHEET cSheetName ] [ AS nCodePage ] |
Don't count on a COPY TO and APPEND FROM pair returning the original file in all configurations. SYLK is limited to 9,999 records while APPENDing. COPYing to most of the spreadsheet formats, like DIF and MOD, introduces a header that may create a blank first record while importing. VFP is limited by the number of rows the spreadsheet supports: 8,192 rows for WK1, WKS, WR1 and WRK formats, and 16,383 for Excel. (Although APPEND FROM can import the 65,536 possible rows of Excel 2000 and later, COPY TO can only output 16,383 rows.) Note that for several of the formats, such as WK3, you can use APPEND FROM, but not COPY TO. |
APPEND FROM SDF has a problem with Date fields: It assumes that dates in the data being appended have two-digit years, so anything after the second digit is assumed to be the start of the next field. DateTime fields are treated correctly. |
"DATAW","LABELLYT","Avery 4143",F,4869,/ / "DATAW","LABELLYT","Avery 4144",F,39266,/ / "DATAW","LABELLYT","Avery 4145",F,24620,/ / "DATAW","LABELLYT","Avery 4146",F,32961,/ /Each character field is surrounded by quotes (the delimiters), and fields are separated by commas. This is the default format for a delimited file. TYPE DELIMITED can handle several other options. DELIMITED WITH BLANK and DELIMITED WITH TAB use quotes for delimiters, and fields are separated by spaces or tabs. DELIMITED WITH DELIMITER lets you specify the delimiter—fields are separated by commas. VFP 5 introduced the DELIMITED WITH CHARACTER clause that lets you specify the separator. So DELIMITED WITH CHARACTER ! means that there's an exclamation point between each pair of fields. We can't see why they couldn't have improved the situation here by giving this option a useful name like SEPARATED BY. Nonetheless, we're very grateful to have this option because it increases the number of files we can handle without having to break out the low-level file functions.You can combine DELIMITED WITH and DELIMITED WITH CHARACTER to specify both the delimiter and the separator.TYPE CSV ("comma-separated values") is like DELIMITED, except VFP assumes the first line in the file contains field names, so it skips that line.On the whole, the choices are quite a mess. If you're still confused by this, we suggest you do what we did to figure all this out. Try COPY TO with each option on a small data sample.The FOR clause of APPEND FROM looks at each record as if it had already been added to the table in question and evaluates the condition in that situation. In older versions of FoxPro, this was a big issue because the deleted flag of a record didn't come along on an APPEND, so it could be very difficult to copy all the deleted or all the undeleted records. Starting in VFP 3, the deleted flag comes along, so this issue just doesn't come up very often. The time you're most likely to run into this is if the table you're copying from has a logical field indicating that it should be copied, but the field doesn't exist in the destination. Say there's an lArchive field in the source indicating that the record is ready to be archived. When you issue a command like:
USE Archive APPEND FROM Source FOR Source.lArchivethe lArchive field is evaluated only for the current record of the source, not for each one in turn. So either all records or no records are copied. Setting a filter on the source table doesn't help; VFP ignores it. The workaround is to use a SQL SELECT statement to select the matching records into a temporary cursor, or COPY the records to a temporary file, and then APPEND from there.The AS clause of both commands lets you specify the code page of the original data so it can be translated on the way in.
Unlike APPEND FROM, IMPORT doesn't set _TALLY to the number of records imported (in our testing, it set _TALLY to 1). Use RECCOUNT() instead. |
If you specify a database to which the new table created by IMPORT is added, and that database has database events turned on in VFP 7, you'd expect the BeforeAddTable and AfterAddTable (or perhaps BeforeCreateTable and AfterCreateTable) events to fire. Unfortunately, no events fire. |
Example |
USE MyTable APPEND FROM MyOtherTable FOR nAmount>500 IMPORT FROM MailList.XLS TYPE XLS |
Usage |
COPY TO FileName [ DATABASE DBCName [ NAME LongTableName ] ] [ FIELDS FieldList | FIELDS LIKE Skeleton | FIELDS EXCEPT Skeleton ] [ Scope ] [ FOR lForCondition ] [ WHILE lWhileCondition ] [ [ WITH ] CDX | [ WITH ] PRODUCTION ] [ NOOPTIMIZE ] [ [ TYPE ] FOXPLUS | FOX2X | DIF | MOD | SDF | SYLK | WK1 | WKS | WR1 | WRK | CSV | XLS | XL5 | DELIMITED [ WITH Delimiter | WITH BLANK | WITH TAB ] [ WITH CHARACTER Separator ] ] [ AS nCodePage ] EXPORT TO FileName [ FIELDS FieldList ] [ Scope ] [ FOR lForCondition ] [ WHILE lWhileCondition ] [ NOOPTIMIZE ] [ TYPE ] DIF | MOD | SYLK | WK1 | WKS | WR1 | WRK | XLS | XL5 [ AS nCodePage ] |
The behavior of COPY TO ... DELIMITED differs between versions of VFP:
|
Earlier versions of Excel permitted a maximum of 16,384 rows. Although more recent versions of Excel extend that limit to 65,536 rows, neither COPY TO nor EXPORT will create an Excel file with more than 16,384 rows. This is because COPY TO only supports XL5 copying, while APPEND FROM and IMPORT support XL8. This is sort of a "by-design" gotcha, but we sure wish they'd change it. |
COPY TO ... TYPE XLS is as much as 10-15 times faster than COPY TO ... TYPE XL5. In addition, XL5 files are significantly larger than their XLS counterparts. However, date fields aren't treated properly in XLS files. You'll see them displayed in Excel as "DD-MMM-YY", and changing the format to "MM/DD/YYYY" doesn't help. You actually have to go into each cell, edit (without making any changes), and then click in another cell to see the date formatted properly. Dates work just fine in XL5 files. |
Example |
USE Labels * Check out some of the options. COPY TO LabelPlus TYPE FOXPLUS COPY TO Label2x TYPE FOX2X COPY TO Label.Txt FIELDS Type, Id, Name, CkVal ; TYPE DELIMITED COPY TO Label.Txt FIELDS Type, Id, Name, CkVal; TYPE DELIMITED WITH BLANK COPY TO Label.Txt FIELDS Type, Id, Name, CkVal; TYPE DELIMITED WITH TAB COPY TO Label.Txt FIELDS Type, Id, Name, CkVal; TYPE DELIMITED WITH ~ COPY TO Label.Txt FIELDS Type, Id, Name, CkVal; TYPE DELIMITED WITH CHARACTER # COPY TO Label.Txt FIELDS Type, Id, Name, CkVal; TYPE DELIMITED WITH ~ WITH CHARACTER # |
See Also |
Append From Array, AfterAddTable, AfterCreateTable, BeforeAddTable, BeforeCreateTable, Copy File, Set Optimize, _Tally |