Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Message from discussion Traversal of a directory tree with housekeeping per directory
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Dave Tweed  
View profile  
 More options Aug 23 2001, 9:20 pm
Newsgroups: comp.lang.perl.moderated
From: Dave Tweed <dtw...@acm.org>
Date: Thu, 23 Aug 2001 21:11:42 -0400
Local: Thurs, Aug 23 2001 9:11 pm
Subject: Re: Traversal of a directory tree with housekeeping per directory

Michael Rolfe wrote:
> I want to traverse a tree but, within each directory, I need to do a fair
> amount of housekeeping.  In particular, I need to read through all the files
> in the directory before I have the information I need to start doing the
> housekeeping in that directory.

Again, I offer my simple tree-walker as an alternative to File::Find. The
compact version recurses into subdirectories as it finds them, so it may
not quite meet your requirements. The full version separates the names before
processing any of them, so you can do something to all the files, do some
housekeeping, and then start recursing into the subdirectories.

Note that in either case, unlike File::Find, these functions do not chdir()
you to the various directories. Instead, you have the full pathname to each
object to manipulate as needed.

Enjoy!

-- Dave Tweed

=========================================================================== ==

#!perl -w
# treewalk.pl - example of walking a directory, for comp.lang.perl.misc

&process_directory ('/path/to/root');

# compact version

sub process_directory {
    my ($path) = @_;

    # get all of the names from the directory, excluding "." and ".."
    local (*DIR);
    opendir (DIR, $path) || die "can't open directory $path: $!";
    my @names = grep (!/^\.\.?$/, readdir DIR);
    closedir DIR;

    # the sort is optional
    for (sort @names) {
        my $temp = "$path/$_";
        if (-d $temp) {
            &process_directory ($temp);
        } else {
            &process_file ($temp);
        }
    }

}

sub process_file {
    my ($path) = @_;

    # whatever ...

}

=========================================================================== ==

#!perl -w
# treewalk.pl - example of walking a directory, for comp.lang.perl.misc

&process_directory ('/path/to/root');

# full version

sub process_directory {
    my ($path) = @_;

    # get the names out of the current directory and separate them into
    # files and subdirectories
    my (@files, @directories);
    my @names = &read_directory ($path);
    for (@names) {
        if (-d "$path/$_") {
            push @directories, $_;
        } else {
            push @files, $_;
        }
    }

    # process all the files
    for (@files) {
        &process_file ("$path/$_");
    }

    # do any housekeeping here, before recursing into subdirectories

    # process all the subdirectories
    for (@directories) {
        &process_directory ("$path/$_");
    }

}

sub process_file {
    my ($path) = @_;

    # whatever ...

}

# customize the filtering and sorting of names here

sub read_directory {
    my ($path) = @_;

    # get all of the names from a directory, excluding "." and ".."
    local (*DIR);
    opendir (DIR, $path) || die "can't open directory $path: $!";
    my @names = grep (!/^\.\.?$/, readdir DIR);
    closedir DIR;

    # optional - filter out all other names starting with '.'
    @names = grep (!/^\./, @names);

    # optional - sort the names
    @names = sort @names;

    @names;

}

=========================================================================== ==

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.