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("java.io.File", &fileName);
 
local string &newFileName;
 
&newFileName = "/newPath/to/File/Origfile.txt.backup";
 
Local JavaObject &fBackup = CreateJavaObject("java.io.File", &newFileName);
 
Local boolean &b2;
 
&b2 = &f.renameTo(&fBackup);
 
If &b2 = True Then
 
/* RENAME SUCCESSFUL */
 
ELSE
 
  /* SOMETHING HAPPENED */
 
END-IF;

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
Author Info
Chris Malek

Chris Malek is a PeopleTools® Technical Consultant with two decades of experience working on PeopleSoft enterprise software projects. He is available for consulting engagements.

About Chris Work with Chris
Looking for pain-free PeopleSoft web services? 😀
PeopleSoft Simple Web Services (SWS)

Introducing a small but powerful PeopleSoft bolt-on that makes web services very easy. If you have a SQL statement, you can turn that into a web service in PeopleSoft in a few minutes.

Book
Integration Broker - The Missing Manual

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