return to first page linux journal archive
keywordscontents

Listing 2. Per Script to Traverse Directories

#!/usr/bin/perl
if((@ARGV != 1) && (@ARGV != 0)) {
print ("\nUsage:  create_vocals [-clean]\n");
print ("\nMP3_ROOT must be set before running" .
       " the script\n\n");
exit(1);
}
if((@ARGV == 1) && !($ARGV[0] eq '-clean')) {
print ("\nIf you supply one argument it must be" .
       " '-clean'\n");
exit(1);
}
$mp3root = $ENV{'MP3_ROOT'};
if(!(-d $mp3root)){
  print ("\nMP3_ROOT not present!!\n");
  exit(1);
}

print ("MP3_ROOT is $mp3root\n");
$vocals = $mp3root . '/.vocals';
if(-d $vocals){
  if(@ARGV == 1){
    print ("Cleaning up the .vocals directory\n");
    chdir( $vocals);
    system ("rm -f *");
    exit(0);
  } else {
print ("\nThe .vocals directory is already in" .
       " $mp3root\n");
print "We will update the files as appropriate\n";
}
} else {
print ("\n.vocals is not there, so we will " .
       "create it.\n");
  mkdir( $vocals, 0777);
}
&recursively_list_files($mp3root);

exit(0);
sub recursively_list_files {
local($dir) = @_;
local($prevdir);
local($directory);
local($file);
local(@directories);
local(@files);
opendir (DIR, $dir) || die (
 "\nCannot open a directory in the tree\n\n");
$prevdir = $dir;
chdir $dir || die (
 "\nCannot find a directory in the tree\n\n");
@directories = grep(!/^\./ , grep (-d ,<\n> 
                  readdir(DIR)));
rewinddir(DIR);
closedir(DIR);
foreach $directory (@directories) {
  $file_to_open = $vocals . "/" . $directory .
    ".txt";
  if(!(-e $file_to_open)){
    open(OUR_FILE, ">$file_to_open");
    $our_changed_directory = $directory;
    $our_changed_directory =~ s/[^a-zA-Z]/ /g;
    print OUR_FILE "$our_changed_directory";
    close( OUR_FILE);
  }
  $output_file = $vocals . "/" . $directory;
  if(!(-e $output_file)){
    system("/usr/lib/festival/examples/text2wave 
      -o $output_file -otype ulaw $file_to_open");
  }
  &recursively_list_files($prevdir . "/" . 
    $directory);
  }
}