in reply to Simple Multiple Folder Search Question

I suggest the following changes: after the my ($File... add the first line, and change the two following:
foreach my $dir (@directories) { #@directories is your list of directo +ries opendir(ROOT,$dir) or die "Directory <$dir> failed: $!"; foreach my $F (readdir(ROOT)) { $File="$dir/$F";
(and don't forget to close the outer loop at the end of your code).

Replies are listed 'Best First'.
Re: Re: Simple Multiple Folder Search Question
by dkaplowitz (Novice) on Mar 22, 2004 at 18:30 UTC
    Thanks to both of you kind souls for the replies. Matija, I tried to alter my code according to your recommendations, but I keep getting a syntax error. Here is what the code looks like now:
    use strict; open OUTPUT, ">c:/temp/warning.txt"; my @directories = ('F:\Solimar_Work_Directories\XVTP\XVTP Cluster Feed +', 'G:\Solimar_Work_Directories\XVTP\XVTP Cluster Feed', 'G:\XVTPInpu +t', 'G:\XVTPInput\DBQA_input', 'G:\XVTPInput\DB_Input'); my $mytime=(time); my ($File,$FileStat,$CallOutDir); foreach my $dir (@directories) opendir(ROOT,$dir) or die "Directory <$dir> failed: $!"; foreach my $F (readdir(ROOT)) { $File="$dir/$F"; foreach $File (readdir(ROOT)) { next if $File=~/^\./; next if ($File eq "_Watched_Folder_._Active_"); next if ($File=~/\._Checking_File_Readiness_$/); if ($FileStat = (stat $File)[9]){ if ($FileStat <= ($mytime - 300)) { print "File older than 5min found. The file name is $File"; print OUTPUT "File older than 5min found. The file name is $Fi +le"; last; } } } } close OUTPUT;

    The error I get is:
    $ perl -c watchdir_revc.pl syntax error at watchdir_revc.pl line 18, opendir" watchdir_revc.pl had compilation errors.
      You probably figured out the syntax error by now, but in case you didn't: the first "foreach" loop (over "my @dir (@directories)") does not have an open curly brace. Fix that, and make sure the other braces are all properly balanced. This is a lot easier if you use a text editor that knows about programming languages (C-style indentation is good enough, but the really good programming editors, which provide syntax highlighting as well as automatic indentation, have a mode for editing Perl.)

      So long as you understand and trust the editor's automatic suggestions about how far indentation should go for a given line, it'll help you to know when and where to look for various brackets, braces, parens and quotes that happen to be missing or misplaced.

      Another problem I've run into is how to differentiate between folders and files when scanning these folders. I noticed that when I started testing this script it would count the sub-folders in its listing of things older than 5 minutes. I'd like to ignore folders completely.
      Is there some kind of regexp I can use in this
      next if $File=~/^\./;
      that would specify a folder to ignore? I'm looking all over for an answer to this and not getting anywhere. Thanks for any help.
        Regexp is not the right test for directory. Directories can have any name. You should use filetests:

        -f $file will return true if $file is a regular file.
        -d $file will return true if $file is a directory.

        Thanks again matija, I seem to have had success using: next if (-d $File);

        I don't think I'll ever learn how to program. This stuff just seems to perpetually elude me.