KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Finding the filename of a given path
PRODUCT: 4D | VERSION: 14.1 | PLATFORM: Mac & Win
Published On: November 5, 2014

Below is an utility method to obtain just the file name of the file when given the path. For example, given the path: "C:\Users\UserName\Example.txt", the function will return just "Example.txt".

// Method: GET_FILE_NAME_FROM_PATH
// Parameters
// $1 - Path of File
//
// Return
// $0 - Filename
// ----------------------------------------------------

C_LONGINT($i;$pos;$length;$lastpos)
C_BOOLEAN($found)
C_TEXT($0;$1;$path;$filename;$pattern;$os)
$path:=$1

$os:=Folder separator
Case of
  : ($os=":") //mac
    $pattern:=$os
  : ($os="\\") //win
    $pattern:=$os+$os
End case
$i:=1
$found:=True
While ($found)
  $found:=Match regex($pattern;$path;$i;$pos;$length)
  $i:=$pos+1
  If ($found)
    $lastpos:=$pos
  End if
End while

$filename:=Substring($path;$lastpos+1)
$0:=$filename