Overlooked features of the Copy File Command

The copy file command on the AS/400 is an often-used and very powerful command. Many people use this command, but few exploit its full potential. Although I am not attempting to define every possible use of the CPYF command, I want to highlight some of the more overlooked and useful features of this command. I use the CPYF command for a variety of purposes, including setting up test data, as another tool for data analysis and debugging purposes, database conversion, etc. Listed below are some examples of some often-overlooked features of the copy file command and some possible reasons why you may want to consider using them.

For files that have external field definitions, this is a good way to pick out records based on values in specific fields.

If you have a keyed physical file, this will cause the copy file command to ignore the key values and copy by relative record number instead of using the index, which will result in substantially shorter times to perform the copy. I do not put keys on physical files, but if you do and you use this technique be aware that the records in your to file will be in relative record number order and not ordered by key. Also, copying by relative record number is sometimes a good way to isolate blocks of records for debug or test data purposes.

FROMKEY(*BLDKEY ('766654' 01)) TOKEY(*BLDKEY ('766654' 99))
This example uses an existing index to copy a file by using key values. Although copies using keys are vastly slower than copies using a relative record number, copying by key values can significantly speed up the copy process if you can use an index to pick out only the records that you want.

This copies all records where the characters "AN" exist, beginning in position 10 of the record. This is especially useful for selecting records from flat files that do not have any external DDS definitions.

Using copy file to print the contents of a file is not the most useful format to look at data, but it does list the relative record numbers of all records printed. I use this primarily for those times when I need to get or examine relative record numbers of a specific record or group of records in a subset. If you have a file that is defined to not reuse deleted records (REUSEDLT (* NO)), this can also give you a clue as to the order in which records were written to a file. But it will do it only if the file hasn't been reorganized based on an existing index or hasn't been the target of a copy file.

This copies all records where the in position 2, or the ORDER# field, the value equals 65. This is especially useful for selecting records based on a partial field value of an externally described file.

The FMTOPT(*MAP *DROP) will copy fields by field name from the fromfile to the tofile. This will not copy any fields that exist in the fromfile that don't exist in the tofile, and it will initialize any fields in the tofile that don't exist in the fromfile. As long as your data type has not changed (numeric to alphanumeric, etc.) for field names that exist in both files, this is an excellent tool to convert data when adding new fields, lengthening existing fields, building work files, etc.

About the author: Tim Granatir is vice president of Technical Services for Interlink Technologies in Maumee, Ohio, and he has 17 years' experience working primarily on the IBM midrange platforms.


This was first published in January 2001

There are Comments. Add yours.

TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

Disclaimer: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.