in reply to ignore certain files in a folder search, and create a random .txt file

# 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

  • Comment on Re: ignore certain files in a folder search, and create a random .txt file
  • Download Code

Replies are listed 'Best First'.
Re: Re: ignore certain files in a folder search, and create a random .txt file
by dkaplowitz (Novice) on Mar 19, 2004 at 21:04 UTC
    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

        Sorry...I guess warning wasn't very specific. When I appended your code to my script, it would tell me that the file that was 5 minutes or older was "." or "..", so I guess that would be another couple of files I'd like to ignore. I am vaguely familiar with grep, but can't see how it would help in this scenario. I will read up on glob to see if that will help.
        Adding your code from this post gives me good feedback on the age of the files in this directory. For instance:
        C:/devl/bin/timedir/1._Checking_File_Readiness_ is > 5 minutes old C:/devl/bin/timedir/2._Checking_File_Readiness_ is > 5 minutes old C:/devl/bin/timedir/3._Checking_File_Readiness_ is > 5 minutes old C:/devl/bin/timedir/4._Checking_File_Readiness_ is > 5 minutes old C:/devl/bin/timedir/5._Checking_File_Readiness_ is > 5 minutes old C:/devl/bin/timedir/6._Checking_File_Readiness_ is > 5 minutes old C:/devl/bin/timedir/_Watched_Folder_._Active_ is > 5 minutes old C:/devl/bin/timedir/Copy (2) of Copy of notsooldfile is > 5 minutes ol +d C:/devl/bin/timedir/Copy of Copy of notsooldfile is > 5 minutes old C:/devl/bin/timedir/Copy of notsooldfile is > 5 minutes old C:/devl/bin/timedir/notsooldfile is > 5 minutes old
        Now if I could tell the script to ignore *._Checking_File_Readiness_ and the file _Watched_Folder_._Active_, I think I'd be on easy street.