http://www.perlmonks.org?node_id=1163495

flieckster has asked for the wisdom of the Perl Monks concerning the following question:

Hello, i have a directory, and subdirectories that i'd like to count files in. i can do that by changing directories and hard coding them to glob and then count the arrays up, but thats no fun!

i've been trying to use file::find to find the image type which in this case is a photoshop file. I got to the point where i can find those files, but i have 2 questions.

1. how do i exclude the invisible files? 2. it gives me a output of filenames, but how to push that into a single array so i can quantify it?
#!/usr/bin/perl -w use Net::FTP; use File::Copy; use Mail::Sender; use Net::SMTP; use File::Find; use POSIX qw(strftime); my $time = strftime("%I:%M:%S",localtime); my $date = strftime("%m-%d-%y",localtime); my $aeropostale = "/Volumes/photorepos/Partners/Aeropostale/post/insto +ck/"; my $psd ="\.psd"; # chdir($aeropostale) or warn "can not get to $aerpostale $!\n"; foreach ($aeropostale) { find( { wanted => \&findfiles, }, "$aeropostale" ); sub findfiles { if (/$psd/){ print "$_\n"; } } }

Replies are listed 'Best First'.
Re: trying to count files in a directory and subs
by stevieb (Canon) on May 19, 2016 at 16:56 UTC

    Does the following code using File::Find::Rule get you closer to what you want?

    use warnings; use strict; use File::Find::Rule; my $dir = 'files'; my $type = '*.psd'; my @files = File::Find::Rule->file() ->name($type) ->in(($dir)); my $count = scalar @files; print "file count: $count\n\n"; print "file names:\n"; print "$_\n" for @files;

    Output:

    file count: 2 file names: files/a.psd files/subdir/b.psd

      yes! that does it.

Re: trying to count files in a directory and subs
by Discipulus (Abbot) on May 19, 2016 at 19:32 UTC
    this is the oldest (previous millemnium) question pronounced in the monastery: see paco's cell..

    As many times in the past i suggest the basic and elegant solution by tachyon. It is simple and you can extend it at will. You'll find the original tachyon's post with plain explanation here

    L*

    There are no rules, there are no thumbs..
    Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.
Re: trying to count files in a directory and subs
by graff (Chancellor) on May 20, 2016 at 03:14 UTC
    I have no idea what you have in mind with using all those modules, but in terms of scanning a directory tree, you might want to look at a script I posted at the Monastery a few years ago: Get useful info about a directory tree.

    On the whole, it's probably a bit off-track (and overkill) for what you want to do (it's a reporting script, with lots of options for what to report), but it covers all the basics, and it's works about as fast as any approach possible in perl. (I use that script almost every day.)