.... while (my $file = readdir(DIR)) { print "$file\n"; if ($file) { ... #### ... while (my $file = readdir(DIR)) { if ($file) { $file_count++; } } ...