common/tools/listdir.pl
changeset 60 9edae8fe1416
child 101 52cc4f7310db
equal deleted inserted replaced
59:c7be4c65f97e 60:9edae8fe1416
       
     1 #!perl -w
       
     2 use strict;
       
     3 
       
     4 my $dir      = shift or die "Usage: $0 <dir> \n";   #  provided dir to traverse
       
     5 my $filelist = [];
       
     6 
       
     7 # fwd declaration to prevent warning
       
     8 sub recursedir($$);
       
     9 
       
    10 # run recurse and print
       
    11 recursedir ($dir, $filelist);
       
    12 print $_, "\n" for(@$filelist);
       
    13 
       
    14 sub recursedir($$) {
       
    15 
       
    16   my $dir  = shift @_;
       
    17   my $list = shift @_;
       
    18 
       
    19   if(opendir(DIR, "$dir")) {
       
    20     #  list dir
       
    21     for my $file(grep { !/^\./ } readdir DIR) {
       
    22       if(-d "$dir\\$file") {
       
    23         #  traverse subdirs
       
    24         recursedir("$dir\\$file", $list);
       
    25       }
       
    26       elsif(-f "$dir\\$file") {
       
    27         #  if file then swap (any present) fwd to bkslash and add to list        
       
    28         $dir   =~s/\//\\/;
       
    29         $file  =~s/\//\\/;
       
    30         push @$list, "$dir\\$file";
       
    31       }
       
    32     }
       
    33     closedir DIR;
       
    34   }
       
    35   else {
       
    36     warn "Cannot open the directory '$dir' $!\n";
       
    37   }
       
    38 }