Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Gather file count in directory

by Anonymous Monk
on Jan 05, 2012 at 12:36 UTC ( #946382=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I have to gather the total files on a directory for which I used the File::Find and Win32::Ole modules , but the results given by these is very slow as the number of disk size increases. What is the fastest way to find total number of files in a directory(I don't need any other statistics except the total files in the directory) ?

Comment on Gather file count in directory
Re: Gather file count in directory
by umasuresh (Hermit) on Jan 05, 2012 at 12:46 UTC

      I am in need of a perl script..

Re: Gather file count in directory
by ~~David~~ (Hermit) on Jan 05, 2012 at 14:22 UTC
    I always like the File::Slurp module.
    use File::Slurp; my $dir = 'somedir'; my @files = read_dir( $dir ); my $num_of_files = scalar @files;
      If you're willing to slurp in the entire filelist, you can also do
      my $num_of_files = scalar @{[<*>]};

      mr.nick ...

      The File::Slurp method is not giving the total number of files in a drive (as in C:// etc). It only gives an upper count of the files in directory doesnt give the count of files in folders ..

Re: Gather file count in directory
by trizen (Friar) on Jan 06, 2012 at 01:20 UTC
    For a decent number of files, you can use:
    sub get_files_count { my ($dir) = @_; opendir my $dir_h, $dir or return 0; # Number of files return scalar grep { -f "$dir/$_" } readdir $dir_h; # Number of files+dirs return scalar @{[readdir $dir_h]} - 2; } print get_files_count('./');
    Or for a larger number of files, you can use:
    sub get_files_count { my ($dir) = @_; opendir my $dir_h, $dir or return 0; my $files = 0; while(defined(my $file = readdir $dir_h)){ if($file eq '.' or $file eq '..'){ next; } # Uncomment the bellow line to count only files # next unless -f "$dir/$file"; ++$files; } closedir $dir_h; return $files; } print get_files_count('./');

      I have tried this subroutine already but it takes a lot of time to count total files as disk size increases(for a drive containing 300gb used space it takes an hour to calculate the total files). I want a method which is a bit faster??

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://946382]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (14)
As of 2014-07-10 17:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (213 votes), past polls