Graphical Listing of File and Folders Using Perl

2teez 0 Tallied Votes 288 Views Share

I wanted a graphical representation of the listing of my files and folders from the command line interface, just like what the CLI tree would give on linux system, I really don't like how the same command is displayed on windows.
So, I called on ma 'Cherie' Perl do it for me.
The posted perl script works fine, even on perl version as old as 5.6.0.
Please feel free to use, comment and modify for your own good.
I would also be willing to help in case anyone what explaination or usage on this code.

Thanks for stoping by, checking and using.....

#!/usr/bin/perl -w
use strict;
use File::Find qw(find);
use File::Spec qw(splitdir);

my $depth           = 0;
my $number_of_dir   = 0;
my $number_of_files = 0;

find( \&wanted, shift || '.' );

print sprintf "%d directories, %d files\n", $number_of_dir, $number_of_files;

sub wanted {
    if ( $_ eq '.' || $_ eq '..' ) {
        print $File::Find::name, $/;
        $number_of_dir--;
    }

    if (-d) {
        ++$number_of_dir;
        $depth = scalar File::Spec->splitdir($File::Find::name);
        print "   |" x $depth, '-- ', $_, '/', $/ unless $_ eq '.' or $_ eq '..';
    }
    else {
        ++$number_of_files;
        print "   |" x ( $depth + 1 ), '-- ', $_, $/;
    }
}