get_unique_filename Subroutine

public subroutine get_unique_filename(stem, tincrement, tnext, istart, filename, ext)

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: stem
logical, intent(in) :: tincrement
logical, intent(in) :: tnext
integer, intent(in) :: istart
character(len=*), intent(out) :: filename
character(len=*), intent(in), optional :: ext

Contents

Source Code


Source Code

    subroutine get_unique_filename(stem, tincrement, tnext, istart, filename, &
                                   ext)

        ! Find a filename which is either the "newest" or the next to be used.
        ! The filename is assumed to be stem.x, where x is an integer.

        ! In:
        !    stem: stem of the filename.
        !    tincrement: the filename is given as stem.x if true, otherwise the
        !        filename is simply set to be equal to stem.
        !    tnext: the next unused filename is found if true, else the
        !        filename is set to be stem.x where stem.x exists and stem.x+1
        !        doesn't and x is greater than istart
        !    istart: the integer of the first x value to check.
        !        If istart is negative, then the filename is set to be stem.x,
        !        where x = |istart+1|.  This overrides everything else.
        !    ext: The file extension. Appended after the numbers.
        ! Out:
        !    filename.

        character(*), intent(in) :: stem
        logical, intent(in) :: tincrement, tnext
        integer, intent(in) :: istart
        character(*), intent(out) :: filename
        character(*), intent(in), optional :: ext

        integer :: i
        logical :: exists

        if(tincrement) then
            i = istart
            exists = .true.
            do while(exists)
                call append_ext(stem, i, filename)
                if(present(ext)) filename = trim(filename)//ext
                inquire(file=filename, exist=exists)
                i = i + 1
            end do
            if(.not. tnext) then
                ! actually want the last file which existed.
                ! this will return stem.istart if stem.istart doesn't exist.
                i = max(istart, i - 2)
                call append_ext(stem, i, filename)
                if(present(ext)) filename = trim(filename)//ext
            end if
        else
            filename = stem
            if(present(ext)) filename = trim(filename)//ext
        end if

        if(.not. tnext) then
            inquire(file=filename, exist=exists)
            if(.not. exists) then
                inquire(file=stem, exist=exists)
                if(exists) then
                    filename = stem
                    if(present(ext)) filename = trim(filename)//ext
                endif
            end if
        end if

        if(istart < 0) then
            call append_ext(stem, abs(i + 1), filename)
            if(present(ext)) filename = trim(filename)//ext
        end if

    end subroutine get_unique_filename