On Sunday 14 Mar 2010 06:47:57 Kenneth Wolcott wrote:
> Hi;
> 
> Re: would like "pure" Perl solution to count of files in directory (nested
> subdirectories too)
> 
> I would like to have a "pure" Perl solution to "find dir | wc -l"
> 

This won't work properly if your filenames contain newlines.

> find2perl x
> 
> *********************************
> #! /usr/bin/perl -w
>     eval 'exec /usr/bin/perl -S $0 ${1+"$@"}'
>         if 0; #$running_under_some_shell
> 
> use strict;
> use File::Find ();
> 
> # Set the variable $File::Find::dont_use_nlink if you're using AFS,
> # since AFS cheats.
> 
> # for the convenience of &wanted calls, including -eval statements:
> use vars qw/*name *dir *prune/;
> *name   = *File::Find::name;
> *dir    = *File::Find::dir;
> *prune  = *File::Find::prune;
> 
> sub wanted;
> 
> # Traverse desired filesystems
> File::Find::find({wanted => \&wanted}, 'x');
> exit;
> 

Add:

<<<
my $count = 0;
>>>

Here.

> sub wanted {
>     my ($dev,$ino,$mode,$nlink,$uid,$gid);
> 
>     (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) &&
>     print("$name\n");

Change this last statement to $count++;

> }
> *********************************
> 
> Ok that's cool...
> 
> But maybe there's a way to change "print" to be "$count++" or something
> like that.
> 
> So I tried that.  I have strict and warnings on and I get: "$filecount
> won't stay shared".

Which code did you try?

> 
> But it comes up with the same result as "find x | wc -l".
> 
> Anybody got a platform-dependent pure-Perl recursive-descent file counter
> (I wanted symlinks and dirs too)?

I normally use File-Find-Object or File-Find-Object-Rule for that.

Regards,

        Shlomi Fish

> 
> I didn't find anything like this on CPAN.
> 
> Thanks,
> Ken Wolcott

-- 
-----------------------------------------------------------------
Shlomi Fish       http://www.shlomifish.org/
"Star Trek: We, the Living Dead" - http://shlom.in/st-wtld

Deletionists delete Wikipedia articles that they consider lame.
Chuck Norris deletes deletionists whom he considers lame.

Please reply to list if it's a mailing list post - http://shlom.in/reply .

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to