Renaming files using PeopleCode

By Chris Malek | Tue, Jul 5, 2016

Here is a code example of how to use PeopleCode and Java to rename or move a file.

We take a file at /path/to/file/origFile.txt and move and rename it to /newPath/to/File/Origfile.txt.backup.

Local string &fileName;
&fileName = "/path/to/file/origFile.txt";
Local JavaObject &f = CreateJavaObject("", &fileName);
local string &newFileName;
&newFileName = "/newPath/to/File/Origfile.txt.backup";
Local JavaObject &fBackup = CreateJavaObject("", &newFileName);
Local boolean &b2;
&b2 = &f.renameTo(&fBackup);
If &b2 = True Then

I end up using this on inbound file processing where you might pickup a file from a directory and you want to move it to a “processed” folder after you have processed the file. You could also delete the file but then the system that generated it may not have an easy way to regenerate the same file. So archiving the processed file is generally safer in case you need to reprocess or do some debugging.

Article Categories
Integration Broker - The Missing Manual

I am in the process of writing a book called "Integration Broker - The Missing Manual" for which you can read online for free.

Read Online