Tech Tip: Utility method to find parent directory of a file/folder
PRODUCT: 4D | VERSION: 14.x | PLATFORM: Mac & Win
Published On: August 7, 2015
Below is an utility method that returns the parent directory of a given file/folder.
C_TEXT($1;$childPath) C_TEXT($parentPath;$sep;$pattern) C_LONGINT($length;$pos;$len) C_BOOLEAN($found) If (Count parameters=1) $childPath:=$1 $length:=Length($childPath) $sep:=Folder separator If ($childPath[[$length]]=$sep) & ($length>1) $childPath:=Substring($childPath;1;$length-1) End if If ($sep="\\") $pattern:="(?mi-s)(\\"+$sep+")(?!.*\\"+$sep+")" Else $pattern:="(?mi-s)("+$sep+")(?!.*"+$sep+")" End if $found:=Match regex($pattern;$childPath;1;$pos;$len) If ($found) $parentPath:=Substring($childPath;1;$pos) End if $0:=$parentPath End if |
Example:
C_TEXT($path;$parent) $path:="C:\\Users\\User1\\File1.txt" $parent:=UTIL_GET_Parent_Directory($path) //$parent returns "C:\\Users\\User1\\" |