sys_file_exists Function

public function sys_file_exists(filepath) result(exists)

Check if a file exists (handles both regular files and symlinks)

Arguments

Type IntentOptional Attributes Name
character(len=*), intent(in) :: filepath

Return Value logical


Source Code

    function sys_file_exists(filepath) result(exists)
        character(len=*), intent(in) :: filepath
        logical :: exists
        integer :: unit, iostat

        ! First try standard inquire
        inquire (file=filepath, exist=exists)

        ! If not found, try opening it (handles symlinks better)
        if (.not. exists) then
          open (newunit=unit, file=filepath, status='old', action='read', iostat=iostat)
            if (iostat == 0) then
                exists = .true.
                close (unit)
            else
                exists = .false.
            end if
        end if
    end function sys_file_exists