http://www.perlmonks.org?node_id=125156
Category: Miscellaneous
Author/Contact Info Jonathan Clover
jclovs
Description: I didn't like how Find:File Module worked for me and what I wanted to do so I created my own recursive program to print out a directory structure of my web directory, for new design purposes. So here it is hope someone finds it useful, and feel free to critque.
#!/usr/bin/perl -w

#####################################################
#####Web Site Directory Print                   #####
#####Copyright 2001, Jonathan Clover            #####
#####Feel Free to Redistribute                  #####
#####                                           #####
#####Description: Program to Print out in plain #####
#####text tab-deliminated representation of a   #####
#####directory structure. Has the ability to not#####
#####include any directory, as well as allows   #####
#####for a specific web directory to be         #####
#####specified within the quiry string formated #####
#####like "?dir=clovs.com/about".               #####
#####################################################

use strict;
use CGI qw(:standard);

my ($web_url, $web_dir, $default_dir, $tab, $header, @non_include);

####################
###Configurations###
####################

#Home Page URL
$web_url = "clovs.com";

#Home Web Directory
$web_dir = "/home/clovs/www";

#Default Directory to Start in if you 
#wish it not to be the Home Web Directory
#$default_dir = "/home/clovs/www";

#Tab String to use for print out
$tab = "\ \ \ \ ";

#CGI Header
$header = "text/html";

#Files not to include
#Those that start with . are never included
#for obvious reasons(aka infinite recursion)
#Regex accepted as values in list
@non_include = ('_',          #Front Page Hidden Folders
        'Merchant2',  #The Online Store Data Folder
        'webstats');  #The Web Statstistics Folder

###########################
###End of Configurations###
###########################

my $cur = CGI->new();
my $start;
my $non_include = '^(\.|'.join('|', @non_include).')';

###Allow for Param's from a web interface###
if ( $cur->param("dir") ){
    my ($nothing, $temp) = split($web_url,$cur->param("dir"));
    $start = $web_dir.$temp;
}
else { $start = $default_dir; }

###Calculate the number of tabs to be
###used when printing out results
my @start = split('/', $start);
my $tabs = $#start;

###Start the program and print out as plain text
print header( $header );
print start_html();

dir_tree($start);

print end_html();

###Subrotines Below###
sub dir_tree {
    my $dir = shift;
    my @dir = split('/', $dir);
    print $tab x ($#dir - $tabs),
          $dir[$#dir]."<br>\n";
    if(-d $dir){
        foreach (op_dir($dir)){
            dir_tree($_);
        }
    }
}

sub op_dir {
    my $dir = shift;
    my @dir;
    opendir(DIR, $dir) || die("Couldn't open dir: $!");
    foreach (sort by_lc readdir(DIR)){
        if ($_ !~ m/$non_include/){
            push(@dir, "$dir\/$_");
        }
    }
    return @dir;
}

sub by_lc {
    lc($a) cmp lc($b);
}