KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: How to get the filename from a path (v11)
PRODUCT: 4D | VERSION: 11.5 | PLATFORM: Mac & Win
Published On: December 18, 2009

Sometimes it may be convienent to quickly get the filename from a path, such as the path stored in the Document variable.

Here is a quick method that can be used to do this:

 ` ----------------------------------------------------
 ` Description
 ` Extract the name of the file from the given path
 `
 ` Parameters
 ` $1 - File path
 ` $0 - File name
 ` ----------------------------------------------------


C_TEXT($0;$1;$path_t;$sep_t;$temp_t)
C_LONGINT($pos_l;$platform_l)

If (Count parameters>=1)

  $path_t:=$1

  $temp_t:=Get 4D folder(Database Folder)
  $sep_t:=$temp_t[[Length(Get 4D folder(Database Folder ))]]

  $pos_l:=Length($path_t)
  If ($pos_l>0)

     ` $pos_l stops at 1 to prevent an error when the
     ` database is compiled with a Range Checking turned on.

    While (($pos_l>1) & ($path_t[[$pos_l]]#$sep_t))
      $pos_l:=$pos_l-1
    End while

    If ($path_t[[$pos_l]]=$sep_t)
       ` The given path contains one or more separators ("\" or ":").
      $0:=Substring($path_t;$pos_l+1)
    Else
       ` The path is suspected to contain only the filename or an empty string.
      $0:=Substring($path_t;$pos_l)
    End if
  Else
     ` The given path is empty
    $0:=$path_t
  End if
End if