By | 05 July 2016

Renaming files using PeopleCode

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

Do you want to become a CI Expert?

If you want to learn more about developing using Component Interface then check out our CI Training Video.