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

Re: [MacPerl-AnyPerl] loop through directories



thank you very much thomas (and ronald) excellent script!

allan


Thomas Wegner wrote:

> >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
>
> [snip]
>
> Hi Allan (again :-),
>
> I recently wrote a little script that steps recursively through
> folders and gathers all filenames.  In principle, it is what you are
> looking for. Note the file test operators '-d' for directory test and
> '-f' for plain file test.
>
> _________________
> #! perl -w
> use strict;
>
> #
> # This is only an example on how to step recursively
> # through folders, adjust it for your needs
> #
>
> #
> # MAIN
> #
>
> my $file = '';
> my @filelist = ();      # all filenames will go into this
>                 # global array
>
> my $txt_folder = 'Harddisk:full:path:to:textfiles:folder';
>
> &check_folders($txt_folder);
>
> foreach $file (@filelist) { # testing 1.. 2.. 3..
>   print "-$file-\n";
> }#for
>
> #
> # SUB(s)
> #
>
> sub check_folders {
>    my($dir) = @_;
>    local (*FOLDER); # use local for filehandles
>
>     # itīs always safer to make the filehandle local
>     # see perlfaq5.pod, Files and FileHandles:
>     # How can I make a filehandle local to a subroutine?
>
>     my(@subfiles, $file, $specfile);
>
>     opendir(FOLDER, $dir) or die "cannot open $dir";
>         # only file and folder names show up, no full paths
>         # adding the maindir is necessary
>     @subfiles = readdir(FOLDER);
>     closedir(FOLDER);
>
>    foreach $file (@subfiles) {
>       $specfile = $dir . ':' . $file;
>       if (-d $specfile) {
>           #
>           # -d test for directory, -f test for file etc.
>           # lookup '-d' in Shuck or find '-X' in perlfunc.pod
>           #
>          &check_folders($specfile); # RECURSION
>       } elsif (-f $specfile) {
>          push(@filelist, $specfile);
>
>          #
>          # do your text line stuff here
>          # (consider to write the lines to the complete.txt file
>          # directly, without buffering in an array)
>          #
>
>       }#if
>    }#for
> }#sub
>
> __END__
>
> Best regards
>
> -- Thomas


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