Last 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]; End-Function; /* Usage */ &FILENAME = GK_FILENAMEGRAB(&FILEPATH); |
Consider that the &FILEPATH string contains the following value.
\\PRD-SERVER1\INTERFACES\AP\OUTPUT\RAW\FILE1.DAT
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.
So return &GK_fpath [&GK_fpath.Len]; will return &GK_fpath [8] ie; FILE1.DAT
Do you know a better way to do this?
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.
hope this helps, francois-xavier
hi francois,
Thanks for the regex code. it was really nice.
thanks,
Raja
Wow…
Glad to see the one line Regex doing what 6 lines of code does!
I just tested your regex at http://www.regextester.com and it works like a charm.
We would love to have people like you write some tips on PSoftSearch.
Thanks a lot…………!
Glad to have helped.
BTW, there’s a better solution using REGEX from Francois-xavier here
I always use a very simple approach: search backwards until you find a slash or backslash:
Good Morning
Need to know how to get the File Server path using peoplecode
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
@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;
SQLExec(“SELECT SRVRHOSTNAME FROM PSSERVERSTAT”, &SERVER);
&sFileDir = GetEnv(“ps_filedir”);
&sPath = “\\” | &SERVER | “\” | &sFileDir | “\” | %DbName | “\”;