« February 2006 | Main | September 2006 »

August 2006 Archives

August 26, 2006

Recursivly print directories in Perl...with no modules

Just for fun I came up with some Perl to recursively print out a directories files and child directories. Why am I posting it? It's not that it's all that difficult or obscure, I just feel like it, and when I did a web search for code on this that doesn't use the File::Find Perl module, I was surprised as to how few results there were. So in the interest of reinventing the wheel a bit, here's what I came up with.

#!/usr/bin/perl use strict; use warnings;

printdir( $ARGV[0] );

sub printdir
{
     my $dir          = $_[0] ? $_[0] : '.';
     my $handle      = ();
     my @dirs      = ();
     my $i           = ();
     
     print "Directory: $dir\n";
     
     opendir( $handle, $dir );
     my @items = readdir( $handle );
     closedir( $handle );
     
     shift @items; #Get rid of .
     shift @items; #Get rid of ..
     
     @items = sort( @items );
     
     while ( $i = shift( @items ) )
     {
          if ( -d "$dir/$i" )
          {
               push( @dirs, $i );
               next;
          }
          print "$dir/$i\n";
     }
     printdir( "$dir/$i" ) while ( $i = shift( @dirs ) );
}

About August 2006

This page contains all entries posted to Avidity Software in August 2006. They are listed from oldest to newest.

February 2006 is the previous archive.

September 2006 is the next archive.

Many more can be found on the main index page or by looking through the archives.