PeopleCode | Copying a file from one directory to another
A. Another Tricky File Task
As noted in previous tips relating to file manipulation, PeopleCode does not provide huge support for file manipulation. Often you may have to resort to using an alternative solution (such as a Java method – refer to tip 050) to perform what you’re after.
This tip covers another common requirement when it comes to files: a file ‘copy’ function. In this case, you simply want to copy a file from one directory to another. Again, there is no native PeopleCode function that will carry out this task. However, rather than fiddling around with the command line, there is a quirky way to do this via the normal attachment functionality. You can upload the source file to a database record via PutAttachment, and then you can download it to another folder via GetAttachment. This provides an easy way to copy a file, irrespective of the operating system.
B. An Example of File Copying
Below is an example of the code to perform the copy function via a file record (make sure to replace the ‘XX_FILE_TMP’ with the appropriate file record). A ‘DeleteAttachment’ is performed at the end to remove the attachment file from the temporary record.
Local string &URL_ID = "record://XX_FILE_TMP"; /* Copy the file to the directory of the current process */ &file_name = "REPORT.pdf"; &source_filepath = GetEnv("PS_SERVDIR") | "\" | &file_name; &dest_filepath = GetCwd() | "\log_output\AE_PROCESS_" | &prcs_instance | "\" | &file_name; /* Copy the source file to the attachment record */ &return = PutAttachment(&URL_ID, &file_name, &source_filepath); /* Copy the file back out to the destination file path */ If &return = 0 Then &return = GetAttachment(&URL_ID, &file_name, &dest_filepath); End-If; /* Delete temp attachment file */ If &return = 0 Then &return = DeleteAttachment(&URL_ID, &file_name); End-If;