KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Finding the path to the parent folder of a given file
PRODUCT: 4D | VERSION: 11.5 | PLATFORM: Mac & Win
Published On: December 3, 2009

Here is a quick method that can be used to find the path to the parent folder of a given file.

` --[HEADERSTART]-------------------------------------
` Description:
` Given a path to a file, get the path to the parent folder.
`
` If a path to a folder is provided and is not terminated by
` a separator, the path to the containing folder will be returned.
`
` Parameters:
` C_TEXT($1) = path to the file
` Return Value:
` C_TEXT($0) = path to the parent folder
` --[HEADEREND]---------------------------------------


C_TEXT($1;$pathToFile)
C_TEXT($0;$pathToParentFolder)

C_TEXT($temp;$separator)
C_LONGINT($pos;$last)

$pathToFile:=$1
$temp:=$pathToFile

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

$last:=0
$pos:=0

Repeat
  $pos:=Position($separator;$temp)
  If ($pos>0)
    $last:=$last+$pos
  End if
  $temp:=Substring($temp;$pos+1)
Until ($pos=0)

$pathToParentFolder:=Substring($pathToFile;1;$last)

$0:=$pathToParentFolder