diff options
-rw-r--r-- | common/utilities.F90 | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/common/utilities.F90 b/common/utilities.F90 index cc2f727..961a349 100644 --- a/common/utilities.F90 +++ b/common/utilities.F90 @@ -272,4 +272,106 @@ contains end subroutine replace_field_int + function get_directories_in_directory(directory) result(res) + implicit none + + character(*), intent(in)::directory + character(1024), dimension(:), pointer::res + character(80)::line + character(len=:), pointer::tempfile + integer::dcount, unum, ierr, i + + tempfile => generate_temporary_filename() + res => null() + + call execute_command_line("ls -l "//trim(directory)//" > "//trim(tempfile), & + wait=.true.) + + open(newunit=unum, file=tempfile, action='read') + dcount = 0 + read(unum, '(A)', iostat=ierr) line + do while(ierr == 0) + if(line(1:1) == 'd') then + dcount = dcount + 1 + end if + read(unum, '(A)', iostat=ierr) line + end do + + close(unum) + + if(dcount > 0) then + allocate(res(dcount)) + + ! Now call ls, but group directories first + call execute_command_line("ls -g "//trim(directory)//" > "//trim(tempfile), & + wait=.true.) + open(newunit=unum, file=tempfile, action='read') + i = 0 + read(unum, '(A)', iostat=ierr) line + do while(ierr == 0 .and. i < dcount) + i = i + 1 + call combine_paths(trim(directory), trim(line), res(i)) + read(unum, '(A)', iostat=ierr) line + end do + + close(unum) + end if + + call unlink(tempfile) + + end function get_directories_in_directory + + function get_files_in_directory(directory) result(res) + implicit none + + character(*), intent(in)::directory + character(1024), dimension(:), pointer::res + character(80)::line + character(len=:), pointer::tempfile + integer::dcount, total_count, unum, ierr, i + + tempfile => generate_temporary_filename() + res => null() + + call execute_command_line("ls -l "//trim(directory)//" > "//trim(tempfile), & + wait=.true.) + + open(newunit=unum, file=tempfile, action='read') + dcount = 0 + total_count = 0 + read(unum, '(A)', iostat=ierr) line + do while(ierr == 0) + if(line(1:1) == 'd') then + dcount = dcount + 1 + end if + total_count = total_count + 1 + read(unum, '(A)', iostat=ierr) line + end do + + close(unum) + + if((total_count - dcount) > 0) then + allocate(res(total_count - dcount)) + + ! Now call ls, but group directories first + call execute_command_line("ls -g "//trim(directory)//" > "//trim(tempfile), & + wait=.true.) + open(newunit=unum, file=tempfile, action='read') + + read(unum, '(A)', iostat=ierr) line + do while(ierr == 0 .and. i < dcount) + i = i + 1 + if(i > dcount) then + call combine_paths(trim(directory), trim(line), res(i-dcount)) + end if + read(unum, '(A)', iostat=ierr) line + end do + + close(unum) + end if + + call unlink(tempfile) + + end function get_files_in_directory + end module utilities
\ No newline at end of file |