KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Utility method that returns the parent directory path for a given file or folder path
PRODUCT: 4D | VERSION: 19 | PLATFORM: Mac & Win
Published On: June 28, 2022

The following is a utility method that takes a path to a file or a folder and returns the parent directory path as the result. The returned path syntax (System or POSIX) will be the same as the input path syntax.

#DECLARE($path_t : Text)->$parentPath_t : Text
var $folder_o : Object

If (Count parameters>=1)

  Case of
    : (Folder($path_t).exists=False) & (Folder($path_t).isFolder)
    : (Folder($path_t).exists) & (Folder($path_t).isFolder)
      $folder_o:=Folder($path_t)
    : (File($path_t).isFile) & (File($path_t).exists)
      $folder_o:=File($path_t)
  End case

  If ($folder_o.parent=Null)
    Case of
      : (Test path name($path_t)=Is a folder)
        $folder_o:=Folder(Convert path system to POSIX($path_t))
      : (Test path name($path_t)=Is a document)
        $folder_o:=File(Convert path system to POSIX($path_t))
    End case
    If ($folder_o.exists)
      If ($folder_o.parent#Null)
        $parentPath_t:=Convert path POSIX to system \
          (Folder($folder_o.parent.path).path)
      End if
    End if
  Else
  If ($folder_o.exists)
      $parentPath_t:=$folder_o.parent.path
    End if
  End if
End if