opendir(D, "unfinished/") or die "unable to open unfinished/: $!\n"; my @files = readdir(D); closedir(D); for (@files) { next unless ($file =~ m/^(\d\d\d\d)-(\d\d)-(\d\d)$/); my ($year, $month, $day) = ($1, $2, $3); eval { ... process download for $year/$month/$day ... ... in case of an error, raise an exception with die ... }; if ($@) { warn "downloading for $year/$month/$date failed: $@\n"; } else { rename("unfinished/$file", "completed/$file"); } }