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

UPDATE: Thanks to ikegami for the solution I needed!

I've got an array full of filenames (including paths) which I'm sorting into categories based on the filepaths. However, one of the categories is also based on the number of files in its folders. For instance, if the filepath contains the word "Paper", then it should be categorized as "paper". However, if there is more than 200 files in that filepath, then it should be categorized as "collection".

Here's what I'm doing now and it's not working correctly. Suggestions?

foreach $file (@file_list) { my $file_path = dirname($file); if ($file_path =~ m/paper/i){ my @files = <$file_path/*>; my $count = scalar(@files); if( $count < 200 ){ $category = "paper"; } else{ $category = "collection"; } } }

Replies are listed 'Best First'.
Re: Counting files in a folder
by ikegami (Patriarch) on Feb 11, 2010 at 19:20 UTC
    You don't convert $file_path from a path to a glob literal. For example, space is special to globs. Does the following work better?
    my @files = glob("\Q$file_path\E/*");
Re: Counting files in a folder
by scorpio17 (Canon) on Feb 11, 2010 at 19:37 UTC

    Are there directories inside the directories? If so, I don't think the file glob method will count them all. It will just count the files inside the top-level directory (it will count directories, too, but not go down inside them).

    I'd do something like this:

    use File::Find; my $category; for my $file (@file_list) { my $file_path = dirname($file); # better make sure this is working! my $count = CountFiles($file_path); if ($count < 200) { $category = "paper"; } else { $category = "collection" } } # Count all files contained inside a directory, recursively sub CountFiles { my $dir = shift; my $count = 0; find( sub { if (-f) { # only count regular files ++$count; } }, $dir); return $count; }

    Note that this way you can do things like not count zero-length files, or ignore symbolic links, etc. by simply changing the file test performed inside the 'find'.

Re: Counting files in a folder
by toolic (Bishop) on Feb 11, 2010 at 18:29 UTC
Re: Counting files in a folder
by lostjimmy (Chaplain) on Feb 11, 2010 at 18:23 UTC
    Can you define "not working"?

    It looks like you aren't using $category correctly. It must be scoped outside of the loop, so when you're finally outside the foreach loop, it just has whatever the last value assigned was. I would think you might want a hash keeping track of the category for each file instead. Something like: $category{$file} = "paper";

      By not working, I mean that it is categorizing the file as "paper" when there are more than 200 files.
        OK well here are some things you can try:
        1. Are you sure you have the right directory in $file_path? File::Basename explicitly states that dirname does not always give the proper director, and that fileparse should be used instead.
        2. Are you sure the file glob is working? Does the @files array actually contain the files you are expecting?
        3. Are you sure you aren't just forgetting to use the $category at the appropriate time like I stated in my last post? I don't see anywhere in your code that you actually use $category, and so after exiting the loop you only have the last assigned value.