Count files in a directory
Type | Intent | Optional | Attributes | Name | ||
---|---|---|---|---|---|---|
character(len=*), | intent(in) | :: | directory |
function sys_count_files(directory) result(count) character(len=*), intent(in) :: directory integer :: count character(len=512) :: command, temp_file, output integer :: unit, iostat temp_file = create_temp_file('sys_count_count', '.tmp') if (get_os_type() == OS_WINDOWS) then command = 'cmd /c dir /a-d /b "'//trim(escape_shell_arg(directory))// & '" 2>nul | find /c /v """" > "'//trim(escape_shell_arg(temp_file))//'"' else command = 'find "'//trim(escape_shell_arg(directory))// & '" -type f 2>/dev/null | wc -l > "'//trim(escape_shell_arg(temp_file))//'"' end if call execute_command_line(command) count = 0 open (newunit=unit, file=temp_file, status='old', iostat=iostat) if (iostat == 0) then read (unit, *, iostat=iostat) count close (unit) end if call sys_remove_file(temp_file) end function sys_count_files