[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

[MacPerl-AnyPerl] loop through directories



hello again,

i need a guideline to modify a script.
basically i have a directory  called "root" that contains subdirectories
which agian might contain even more subdirectories - all these
directories, apart from "root" contain image-files. i also have a
directory called "txtfiles" which contains txt-files and any number of
subdirectories containg further txt.files. these txt.files are html,
txt, asp etc.

i want to extract all image information from within these txt.files,
then check if the images indeed exist in their respective directories
and if so copy them into correct directories  all
into a directory called"copied";

my approach can be seen in the script below, where i collect all lines
of the txt.files (only in the txtfiles directory itself and possibly one
(1) subdirectory) into one large txt-file called
"complete.txt" for later use.

i think iīm able to do this the hard way , but my question is, isnīt
there a smart way to loop through any possible subdirectory, then exit
the loop when no more directories present,
instead of doing this construction:?

if  (this is a directory)
{opendir
    if(this is a txt.file
        {read lines}
    if  (this is a directory)
        {opendir
            if(this is a txt.file
                {read lines}
  and so on forever ...
}

thanks allan


#!/pack/collect/bin/perl -w

#macintosh version#

$path = "root/images"; # general path to image directory
$rootname = $path;
$rootname =~ s/(.+)\/\w+/$1/ig; # extract root directorty name

opendir(COPYMASTER, "txtfiles") or die "unable to open txtfiles";
@nomast = readdir(COPYMASTER);
closedir(COPYMASTER);

foreach $dirfile(@nomast)
 {

 if ($dirfile =~ /\.\w+/ig) # check that this is not a directory but a
file
  {
  open(TXT, ":txtfiles:$dirfile") or die "cant open $dirfile"; # open
file
  @inside = <TXT>;

  foreach $line(@inside) # read all lines
   {
   push(@directlist, $line) # collect all lines
   }
  }

 if ($dirfile =~ /[^\/\.]+/ig)  # check that that this is indeed a
directory
  {
  opendir(COPY, ":txtfiles:$dirfile") or die "unable"; #open directory
  @anydirectory = readdir(COPY);
  closedir(COPY);

  foreach $subfile(@anydirectory)
   {
   if ($subfile =~ /\.\w+/ig) # check that this is not a directory but a
file
    {
    open(TXT, ":txtfiles:$dirfile:$subfile") or die "cant open"; #open
file
    @deepinside = <TXT>;
    close(TXT);

    foreach $line(@deepinside) #read lines
     {
     push(@directlist, $line); #collect lines
     }
    }
   }
  }
 }

open(FOUR, ">complete.txt") ; #write all lines to this file
print FOUR @directlist;
close(FOUR);

 use File::Copy;
 @ARGV = "complete.txt";
 while(<>)
  {
  chomp;
  s/.*?=// or next;
  s/^"// and s/"$//;
  ($dir, $file) = /(\w+)?\/?([^\/]+\.(gif|jpg|jpeg))/i or next; #extract
the directory name in which the image-file exists
  mkdir(":copied:$rootname", "0666"); # create root in directory called
copied
  mkdir(":copied:$rootname:$dir", "0666"); # create any subdirectory
that holds an image-file
  -e ":pic:$rootname:$dir:$file" or next; #check if image-file indeed
exists
  copy (":pic:$rootname:$dir:$file", ":copied:$rootname:$dir:$file");
#copy file into mirrored location
  }







==== Want to unsubscribe from this list?
==== Send mail with body "unsubscribe" to macperl-anyperl-request@macperl.org