Creating a Unique File Name

If you are working with files or file attachments in PeopleCode, you will typically want to create a unique file name for each user to prevent them from overwriting each others files if files are being stored in the same folder.

Here's a simple piece of PeopleCode that generates a file name prefix with the format OPRID-Date-Time. Note that the date and time formats will be specific to your region.

Local string &sFilePrefix;
 
&sFilePrefix = %OperatorId | "-";
&sFilePrefix = &sFilePrefix | String(%Date) | "-";
&sFilePrefix = &sFilePrefix | Substring(String(%Time), 1, 8) | "-";

The Substring(String(%Time), 1, 8) drops the millisecond component of the time stamp (which is 6 digits long). If you want to include the millisecond component, don't perform the Substring. Note also that %Time is the database time not the application server time. If you want the application server time use %PerfTime instead. The application server time %PerfTime might be more accurate for file attachments for example.

If you have a number of files per each user, you might want to consider creating a subdirectory for each each operator ID and placing files in there.

Note that the reason for using Operator IDs is that they have a strict format and don't contain any spaces.

This isn't the case for run control IDs. For example you can create run control IDs like these:

  • try saving this \/:*?"<>|
  • rm -r -f /

Not the most ideal file names! That is why run control ID is not used in the file prefix.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License