xHarbour Reference Documentation > Command Reference |
Exports records from the current work area to a database or an ASCII text file.
COPY TO <targetFile> ; [FIELDS <fieldNames,...> ;] [<Scope> ; ] [WHILE <lWhileCondition> ;] [FOR <lForCondition> ;] [VIA <rddName>] [SDF | DELIMITED [WITH BLANK | TAB | PIPE | <xDelimiter> ] ] [CODEPAGE <cCodePage>] ; [CONNECTION <nConnection>]]
When no RDD is specified, the RDD active in the current work area is used to open the external file. If the VIA clause is used, the specified RDD must be linked to the application. Use the REQUEST command to force an RRD be linked.
<xDelimiter> can also be specified as an array with two elements: { <cCharacterDelimiter>, <cFieldDelimiter> }. If this option is used, the array must be enclosed in parentheses. It defines the delimiting characters for field values of type "C" and the delimiters between field values.
Important: If the DELIMITED WITH option is used in the COPY TO command, it must be placed as the last option in the command.
COPY TO is a database command used to export data of the current work are into a second file. Exported records are added to the end of the target file. The target file receives data from all fields of the current work area, unless a list of fields is explicitely specified.
The file in the current work area can be opened in SHARED mode. COPY TO creates the target file and opens it in EXCLUSIVE mode while data is being exported.
When the target file is an ASCII text file, all date values are written as a string in the format yyyymmdd. Other specifications for supported ASCII formats ar listed in the folowing tables:
SDF Text File
File Element | Format |
---|---|
Character fields | Padded with trailing blanks |
Date fields | yyyymmdd |
Logical fields | T or F |
Memo fields | Ignored |
Numeric fields | Padded with leading blanks or zeros |
Field separator | None |
Record separator | Carriage return/linefeed |
End of file marker | 0x1A or CHR(26) |
DELIMITED Text File
File Element | Format |
---|---|
Character fields | May be delimited, with trailing blanks truncated |
Date fields | yyyymmdd |
Logical fields | T or F |
Memo fields | Ignored |
Numeric fields | Leading zeros may be truncated |
Field separator | Comma |
Record separator | Carriage return/linefeed |
End of file marker | 0x1A or CHR(26) |
DELIMITED WITH BLANK Text File
File Element | Format |
---|---|
Character fields | Not delimited, trailing blanks may be truncated |
Date fields | yyyymmdd |
Logical fields | T or F |
Memo fields | Ignored |
Numeric fields | Leading zeros may be truncated |
Field separator | Single blank space |
Record separator | Carriage return/linefeed |
End of file marker | 0x1A or CHR(26) |
Deleted records: Records that are marked for deletion in the current work area are ignored when SET DELETED is set to ON. These records are not exported. With SET DELETED OFF, all records matching the import scope are exported and the deleted flag is set accordingly in the target file.
See also: | APPEND FROM, COPY FILE, COPY STRUCTURE |
Category: | Database commands |
Source: | rdd\dbcmd.c, rtl\dbdelim.prg |
LIB: | xhb.lib |
DLL: | xhbdll.dll |
// The example creates a subset of records from a database // within an intermediate file. PROCEDURE Main USE Address NEW COPY TO Tmp FOR "LONDON" $ Upper(CITY) USE Tmp Browse() USE ERASE Tmp.dbf // delete intermediate file RETURN
// The example creates different delimited ASCII files. PROCEDURE Main USE Address NEW // Creates a regular DELIMITED ASCII file COPY TO Test.txt DELIMITED // Uses Chr(9) as field delimiter COPY TO Test1.txt DELIMITED WITH TAB // Uses "|" as field delimiter COPY TO Test2.txt DELIMITED WITH PIPE // Encloses character values in single quotes and separates // fields with Chr(255) COPY TO Test3.txt DELIMITED WITH ( {"'", Chr(255) } ) USE RETURN
http://www.xHarbour.com