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"

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;

sub wanted {
    my ($dev,$ino,$mode,$nlink,$uid,$gid);

    (($dev,$ino,$mode,$nlink,$uid,$gid) = lstat($_)) &&
    print("$name\n");
}
*********************************

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".

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 didn't find anything like this on CPAN.

Thanks,
Ken Wolcott

Reply via email to