CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Dir Listing with subfolders

 



HTTP404
Deleted

Mar 12, 2001, 12:25 PM

Post #1 of 3 (225 views)
Dir Listing with subfolders Can't Post

how can a make listing of dir i mean easy way

ofcourse i could use -d to check type if it's dir or file then make few loops to make same inside dirs

but it has to be easy Perl ay

thnx





japhy
Enthusiast

Mar 12, 2001, 12:48 PM

Post #2 of 3 (222 views)
Re: Dir Listing with subfolders [In reply to] Can't Post

This came up on Perl Monks a few days ago. The suggested solution is to use File::Find, a builtin module that handles the directory recursing for you.

Building a data structure out of this directory listing is another matter. I produced one solution which created a hash structure like:


Code
$dirtree = { 
filename => size,
filename => size,
directory => {
filename => size,
# ...
},
# ...
}

The code used to produce that structure is:


Code
#!/usr/bin/perl -w 

use File::Find;
use Data::Dumper;
use strict;

$Data::Dumper::Indent = 1;

build_tree(my $tree, shift);
print Dumper $tree;


sub build_tree {
my $node = $_[0] = {};
my @s;
find( sub {
$node = (pop @s)->[1] if @s and $File::Find::dir ne $s[-1][0];
return $node->{$_} = -s if -f;
push @s, [ $File::Find::name, $node ];
$node = $node->{$_} = {};
}, $_[1]);
$_[0]{$_[1]} = delete $_[0]{'.'};
}

You pass that program the directory to create a tree for.

Jeff "japhy" Pinyan -- accomplished hacker, teacher, lecturer, and author


HTTP404
Deleted

Mar 12, 2001, 12:52 PM

Post #3 of 3 (221 views)
Re: Dir Listing with subfolders [In reply to] Can't Post

thnx friend for reply in 4 min


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives