Function – Separate Filename from File Path

PeopleCode Filename SplitLast week, we were working on an App Engine that called an encryption script on the Windows server using Exec command. This script required us to send the filename to be encrypted. But what we had in our State Record was a string that contained the complete path of the file which had the filename towards the end.

We had to capture the filename into a string and then pass it on to the script for encryption. The simplest way that came to my mind was to use the split function to split the filepath+filename string into an array and then take the last element which would be the actual filename. We later wrote a function that does this for later use.

Function GK_FILENAMEGRAB(&GK_FILEPATH) Returns string;
   /* Function to separate the filename from filepath */
   Local Array of String &GK_fpath;
   &GK_fpath = Split(&GK_FILEPATH, "\");
   Return &GK_fpath [&GK_fpath.Len];
/* Usage */

Consider that the &FILEPATH string contains the following value.

The split function would split the filepath into its individual components.
So the statement &GK_fpath = Split(&GK_FILEPATH, “\”); will have the following values in the array.

PeopleCode Split Function

So return &GK_fpath [&GK_fpath.Len]; will return &GK_fpath [8] ie; FILE1.DAT

Do you know a better way to do this?

Subscribe to Comments RSS Feed in this post
11 Responses
  1. hi
    i love regular expressions and have found multiple ways to put them in peoplecode. Below is the code i would use for your purpose. The regex removes anything before the last slash or backslash.

    Local string &filename = CreateJavaObject("java.lang.String", &path).replaceFirst(".*[/]", "");

    hope this helps, francois-xavier

    • hi francois,

      Thanks for the regex code. it was really nice.


    • Wow…

      Glad to see the one line Regex doing what 6 lines of code does!
      I just tested your regex at and it works like a charm.

      We would love to have people like you write some tips on PSoftSearch.

  2. Thanks a lot…………!

  3. I always use a very simple approach: search backwards until you find a slash or backslash:

    For &i = Len(&sFullPath) To 1
       If Substring(&sFullPath, &i, 1) = "/" Or
             Substring(&sFullPath, &i, 1) = "" Then
          &sfilename = Substring(&sFullPath, &i, 1) | &sfilename
  4. Good Morning

    Need to know how to get the File Server path using peoplecode

  5. Or, to use delivered code:

    Declare Function GetFileNameFromPath PeopleCode PSXPFUNCLIB.FUNCLIB FieldFormula;

    &sFileName = GetFileNameFromPath(&sFullPath);

    • It’s good to get a fresh way of lokinog at it.

    • Thank’s

  6. @ramakrishna

    You could do something like the following, depending on how your servers are laid out and OS, of course. I’m assuming NT, but you could always use Record.PSXPFUNCLIB.FUNCLIB’s GetDirSeparator() instead:

    Local string &SERVER, &sFileDir, &sPath;

    &sFileDir = GetEnv(“ps_filedir”);
    &sPath = “\\” | &SERVER | “\” | &sFileDir | “\” | %DbName | “\”;

Leave a Reply

Your email address will not be published. Required fields are marked *