The directory containing path (/a/b for /a/b/c), or '' if path has no parent.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| character(len=*), | intent(in) | :: | path |
a file or directory path |
pure function parent_directory(path) result(parent) !! The directory containing `path` (`/a/b` for `/a/b/c`), or `''` if `path` has no parent. character(len=*), intent(in) :: path !! a file or directory path character(len=:), allocatable :: parent integer :: n, k n = len(path) do while (n > 1) ! ignore trailing separators if (index('/\', path(n:n)) == 0) exit n = n - 1 end do k = scan(path(1:n), '/\', back=.true.) if (k == 0 .or. n <= 1) then parent = '' else if (k == 1) then parent = path(1:1) ! the root else if (k == 3 .and. path(2:2) == ':') then parent = path(1:3) ! a drive root, `C:\` else parent = path(1:k - 1) end if end function parent_directory