Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Gather file count in directory

by trizen (Hermit)
on Jan 06, 2012 at 01:20 UTC ( #946513=note: print w/replies, xml ) Need Help??


in reply to Gather file count in directory

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('./');

Replies are listed 'Best First'.
Re^2: Gather file count in directory
by Anonymous Monk on Jan 06, 2012 at 06:27 UTC

    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: note [id://946513]
help
Chatterbox?
[Discipulus]: better to ask a full SOPW
erix self-flagellates with the cluebat
[erix]: indeed had to be( time make check-world ) > test_run2.output 2>&1
[erix]: probably misremembered because of 2>&1 |
[erix]: oh nice, can do |& nowadays. Learned something new after all
LanX flagelates erix with a signed markov chain

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (10)
As of 2017-03-23 08:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should Pluto Get Its Planethood Back?



    Results (285 votes). Check out past polls.