Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Recursive file/folder indexing

1 view
Skip to first unread message

Scott Edward Skinner

unread,
Oct 27, 1999, 3:00:00 AM10/27/99
to
I'm looking for a simple example of a Perl script that takes a folder path
as an argument and iterates through every nested file and folder in that
path, all the way down.

I don't need a complete program, just the minimum necessary to illustrate
the basic recursive concepts.

All help is appreciated.

-S

Sam Holden

unread,
Oct 27, 1999, 3:00:00 AM10/27/99
to

Here's a complete program that doesn't do much except print everything out:

perl -lMFile::Find -e'find(sub{print},$ARGV[0])' <insert directory path here>

I suggest you look at the File::Find code if you want to see the basic
concepts (though it does the advanced things as well... checking for
symlinks, etc).


--
Sam

If your language is flexible and forgiving enough, you can prototype
your belief system without too many core dumps.
--Larry Wall

Jonathan Stowe

unread,
Oct 28, 1999, 3:00:00 AM10/28/99
to
On Wed, 27 Oct 1999 19:10:51 -0400 Scott Edward Skinner wrote:
> I'm looking for a simple example of a Perl script that takes a folder path
> as an argument and iterates through every nested file and folder in that
> path, all the way down.
>
> I don't need a complete program, just the minimum necessary to illustrate
> the basic recursive concepts.
>

Of course you will really want to use the module File::Find in real life
but just for shits and giggles - here is something I knocked up when
someone asked about creating a directory tree - this outputs nested HTML
lists showing the directory structure and the files - the reason *I*
didnt use File::Find was that it seemed too difficult to recursively
process the tree structure into an array as seemed natural - of course
you will almost certainly run out of memory in a deep tree with lots
of files and has *ahem* undefined behaviour in the face of symbolic links
and all those other nasty things that File::Find shields you from ...


#!/usr/bin/perl -w

use strict;

my $input_dir = $ARGV[0] || '.' ;

MAIN:
{
my @tree;
dirwalk ($input_dir,\@tree);
print "<OL>\n";
printtree(\@tree);
print "</OL>\n";
}

sub dirwalk
{
my ($dir,$tree) = @_ ;

push @{$tree},($dir =~ m#([^/]+$)#);

opendir DIR, $dir || die "Couldnt open $dir - $!\n";
my @entries = grep !/^\.{1,2}$/, readdir(DIR);
closedir (DIR);

foreach my $item (@entries)
{
my $fullpath = "$dir/$item";

if ( -d $fullpath )
{
dirwalk ( $fullpath,\@{$tree->[@{$tree}]});
}
else
{
push @{$tree},$item;
}
}
}

sub printtree
{
my $tree = shift;

print "<LI>",shift @{$tree},"/\n";
print "<OL>\n";
foreach my $item ( @{$tree} )
{
if (ref $item eq "ARRAY" )
{
printtree($item);
}
else
{
print "<LI>\n";
print $item,"\n";
}
}
print "</OL>";
}

Have fun ...

/J\
--
Jonathan Stowe <j...@gellyfish.com>
<http://www.gellyfish.com>
Hastings: <URL:http://dmoz.org/Regional/UK/England/East_Sussex/Hastings>

0 new messages