Find::Find

Discussion in 'Perl' started by Dan Jones, Oct 7, 2004.

  1. Dan Jones

    Dan Jones Guest

    I'm writing a script to process a directory tree of images.  In each
    directory, I need to process each image and generate an HTML file listing
    all of the images and links to the subdirectories.

    Just about every source I can find on the 'net for processing subdirectories
    points you at Find::Find.  However, I'm trying to do something like this:

    enter directory
    open INDEX, ".\index.html"
    print INDEX HTMLheader
    foreach file{
            if(file is an image){
                    process file
                    print INDEX FileInfo
            }
            else if(file is a directory){
                    print INDEX directorylink
            }
    }
    print INDEX HTMLfooter
    close INDEX
    next directory

    I need to be able to deal with a full file tree - multiple subdirectories
    several levels deep.

    As near as I can tell, Find::Find was not designed for this.  It's only
    capable of running a command on each file, not processing files and file
    names in batches as I'm trying to do.  Am I missing some of Find::Finds
    capabilities?  Or do I need to roll my own functions for processing
    subdirectories recursively?
     
    Dan Jones, Oct 7, 2004
    #1
    1. Advertising

  2. Dan Jones

    Joe Smith Guest

    Dan Jones wrote:

    > As near as I can tell, Find::Find was not designed for this. It's only
    > capable of running a command on each file, not processing files and file
    > names in batches as I'm trying to do. Am I missing some of Find::Finds
    > capabilities?


    Yes, you're missing the 'preprocess' and 'postprocess' hooks that I added
    to File::Find back in 2000.

    Here's how to get all the file names and then process as a single batch:

    #!/usr/bin/perl
    use strict;
    use warnings;
    use File::Find;
    use vars qw/*name/;
    *name = *File::Find::name;
    our(@files,@dirs);
    find( sub {if(-d $_){push @dirs,$name}else{push @files,$name}}, 'Olympus');
    print "Directories: @dirs\nFiles: @files\n";



    Here's how to process the beginning and the end of directories, recursively.

    #!/usr/bin/perl
    use strict;
    use warnings;
    use File::Find;
    use vars qw/*name *dir/;
    *name = *File::Find::name;
    *dir = *File::Find::dir;

    my %options = (
    wanted => \&wanted,
    preprocess => \&pre,
    postprocess => \&post,
    );

    our %info;
    find \%options, qw(Olympus Canon); # Subdirectories full of pictures
    print "$info{$_}\n\n" for sort keys %info;
    exit;

    sub pre { # Called before entering a directory
    $info{$dir} = "\t DIR: $dir\n";
    sort @_; # Pre-process directory entries by sorting them
    }

    sub wanted { # Called for each directory entry
    $info{$dir} .= (-d $_) ? "Dir: $_\n" : "File: $_\n";
    }

    sub post { # Post-process dir by appending END
    $info{$dir} .= "\t END: $dir\n";
    }


    -Joe

    P.S. Don't post to comp.lang.perl; post to comp.lang.perl.misc instead.
     
    Joe Smith, Oct 8, 2004
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ad
    Replies:
    2
    Views:
    862
  2. Peter Hansen
    Replies:
    6
    Views:
    416
    Peter.....
    Sep 29, 2004
  3. Wybo Dekker
    Replies:
    1
    Views:
    366
    Yukihiro Matsumoto
    Nov 15, 2005
  4. vdvorkin
    Replies:
    0
    Views:
    414
    vdvorkin
    Feb 10, 2011
  5. vdvorkin
    Replies:
    3
    Views:
    816
    vdvorkin
    Feb 14, 2011
Loading...

Share This Page