There is [a subroutine called 'list_files' in fpm package](https://fortran-lang.github.io/fpm/proc/list_files.html).
program main
use fpm_strings, only: string_t, str_ends_with
use fpm_filesystem, only: list_files
implicit none
type(string_t), allocatable :: files(:)
integer :: i
call list_files("my_dir", files, .false.)
do i=1, size(files)
if (str_ends_with(files(i)%s, "txt")) then
print *, files(i)%s
endif
enddo
end program main