Skip to content

Instantly share code, notes, and snippets.

@AndrewRussellHayes
Created November 20, 2013 16:43
Show Gist options
  • Save AndrewRussellHayes/7566490 to your computer and use it in GitHub Desktop.
Save AndrewRussellHayes/7566490 to your computer and use it in GitHub Desktop.
Accepts: directory path Does: counts files in directory ignoring dotfiles and files named "file_list" Returns: count as an int Note: this is probably the fastest "pure perl" way to do this. It is certainly faster than reading the whole file in to an array and then processing through the array. This just processes as the directory is read.
sub getCount{
my $DH; # Directory Handle
my $WD = $_[0]; # Working Directory
my $DE; # Directory Entry (file)
my $CT; # Count
$CT=0;
opendir($DH, $WD) or die "Cannot open working directory |$WD|: $!";
while ($DE= readdir($DH))
{
next if $DE=~ /^\./ || $DE=~ /file_list/;
$CT++;
}
closedir($DH);
return $CT;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment