Back to Code & Script Bits Back to chipwork.com
 
Listing Windows directories recursively with Perl.
This script will output a list of all files in the root directory
(specified by '$dir') and in any subdirectories within it.
In one form it makes a CSV list from an mp3 collection as part
of a media collection database interface, and in another it's used
for converting Windows Favorites to HTML pages.
 

#!perl

$dir = "\\\\machine_name\\and\\path";

&listdirectory($dir);

sub listdirectory
{
 local($dir);
 local(@lines);
 local($subdir);
 local($lvl_counter);
 local($list_length);

 $dir = $_[0];
 if(opendir (DIR, $dir))
 {
   @lines = readdir (DIR);
   closedir (DIR);
   $lvl_counter = 2;
   $list_length = ( scalar @lines );
   while ($lvl_counter < $list_length)
   {
      $subdir = $dir."\\".$lines[$lvl_counter];
      if(opendir (SUBDIR, $subdir))
      {
         closedir (SUBDIR);
         &listdirectory($subdir);
      }
      else
      {
         &processnames($subdir);
      }
      $lvl_counter++;
      }
   }
}

sub processnames {
$filecount++;
print ("$filecount $_[0]<br>");
}

Windows Favorites to HTML   |   Perl CSV List Maker

Back to chipwork.com.