basename Function

public function basename(path, suffix) result(base)

Extract filename from path with/without suffix

Arguments

Type IntentOptional AttributesName
character(len=*), intent(in) :: path
logical, intent(in), optional :: suffix

Return Value character(len=:), allocatable


Contents

Source Code


Variables

TypeVisibility AttributesNameInitial
character(len=:), public, allocatable:: file_parts(:)
logical, public :: with_suffix

Source Code

function basename(path,suffix) result (base)

    character(*), intent(In) :: path
    logical, intent(in), optional :: suffix
    character(:), allocatable :: base

    character(:), allocatable :: file_parts(:)
    logical :: with_suffix

    if (.not.present(suffix)) then
        with_suffix = .true.
    else
        with_suffix = suffix
    end if

    if (with_suffix) then
        call split(path,file_parts,delimiters='\/')
        if(size(file_parts).gt.0)then
           base = trim(file_parts(size(file_parts)))
        else
           base = ''
        endif
    else
        call split(path,file_parts,delimiters='\/.')
        if(size(file_parts).ge.2)then
           base = trim(file_parts(size(file_parts)-1))
        else
           base = ''
        endif
    end if

end function basename