dkaplowitz has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I am trying to ignore/omit some files from the script below that searches a folder for any files older than 5 minutes (See comments in script). I also need the script to spit out warning.txt file in C:\temp when it finds a file older than 5 minutes that is not among the ones to be ignored.
#!/bin/perl -w use strict; my $mytime=(time); my ($File,$FileStat,$CallOutDir); chdir 'C:/devl/bin/timedir' or die "Arghh!!! $!"; opendir(ROOT, ".") or die "Arghh!!! $!"; foreach $File (readdir(ROOT)) { next if $File=~/^\./; # I need to ignore one specific file from + this list here and ignore *.filetype-x as well if ($FileStat = (stat $File)[9]){ if ($FileStat <= ($mytime - 300)) { print "File older than 5min found. The file name is $File"; #create C:\temp\warning.txt here.... last; } } }
Thanks for any guidance. Dave
  • Comment on ignore certain files in a folder search, and create a random .txt file
  • Download Code

Replies are listed 'Best First'.
Re: ignore certain files in a folder search, and create a random .txt file
by tachyon (Chancellor) on Mar 19, 2004 at 20:47 UTC
    # skip a file in the loop in this case .gif files next if $File =~/\.gif$/; # write to a file (append mode) my $file = "C:\\temp\\warning.txt"; open OUT, ">>$file" or die "Can't append $file, perl say reason $!\n"; # print to file print OUT "$File older than 5min found\n"; # warn prints to STDERR ie screen on win32 warn "$File older than 5min found\n"; close OUT;

    cheers

    tachyon

      Thanks for the reply, tachyon. I tried adding that code, thus:
      #!/bin/perl -w use strict; my $mytime=(time); my ($File,$FileStat,$CallOutDir); chdir 'C:/devl/bin/timedir' or die "Arghh!!! $!"; opendir(ROOT, ".") or die "Arghh!!! $!"; foreach $File (readdir(ROOT)) { next if $File=~/^\./; next if $File =~/\._Checking_File_Readiness_$/; # This is the *.fi +letype I want to ignore next if $File =~/\_Watched_Folder_Active_$/; # This is the single +file I'd also like to ignore if ($FileStat = (stat $File)[9]){ if ($FileStat <= ($mytime - 300)) { print "File older than 5min found. The file name is $File"; #send mail here.... last; } } }
      but now I get a warning based on the "." and "..", which I'd also like to ignore (I thought the first script I posted ignored that. Pardon me if I didn't append your code correctly...I'm obviously quite new to all this. Thanks for any input. Dave

        What warnings exactly? It should not give warnings as far as I can see. You need to be specific. There are really easy ways to do this stuff in perl. grep and glob are quite handy tools.....

        # in simple pieces my $five_min = 5 / 60*24; # 5 minutes as part of day for M time my $dir = 'C:/devl/bin/timedir'; my @files_dirs = glob("$dir/*"); my @files = grep{ ! -d $_ } @files_dirs; my @want = grep{ ! m// and ! m// } @files; my @old = grep{ -M $_ > $five_min } @want; print "$_ is > 5 minutes old\n" for @old; # in one line: print "$_ is > 5 minutes old\n" for grep { ! -d and ! m/foo/ and ! m/bar/ and -M > 0.0035 } glob("C:/devl/bin/timedir/*");

        cheers

        tachyon

Re: ignore certain files in a folder search, and create a random .txt file
by chip (Curate) on Mar 19, 2004 at 20:55 UTC
    I wouldn't be surprised if that chdir() call didn't do what you think it does. Win32's idea of chdir doesn't necessarily involve changing the current drive.

        -- Chip Salzenberg, Free-Floating Agent of Chaos