Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Perl -M to check file modified time not working

Status
Not open for further replies.
Jun 3, 2007
84
US
Hello everyone I am not sure why using the -M to check for files modified after a certain time is not working. Regardless of what time I change the < xxx entry to I get files that don't match that time criteria. Can someone please point me in the right direction, or let me know if there is a better way to accomplish this?

Pretty much all I am trying to do is read files in a directory and only process files that are older than xx time (60) minutes in this example.

thanks for the help in advanced.

Code:
opendir (DIR, "$DirPath") or die "Cannot open $DirPath $!\n";
my @Files = grep { !/^\.{1,2}$/ } readdir DIR;
close DIR;
foreach my $file (@Files) {
    chomp $file;
    print $file, "\n";
    if ((-M "$file") < .0416) {
        do something
    } else {
        print "No file(s) found that is older than .0416\n";
    } 
}
 
Firstly, make sure the file exists.

Second, if you want it to be older than 60 minutes, you need -M to return a number greater than 0.0416, not less than it.
 
Thanks for the quick reply.

the print statement that I am doing DOES show that files are present.

Changing the < .0416 to > .0416 still shows in correct results.

In the directory I have placed two files one that is always gt a time I am using to test the script and on that is lt the time in the script. Neither work, regardless of changing the < > pointer or the times.

Thanks again for the help.
 
I'd never rely on print statements to see if a file exists. Use -f or -e instead. I would expect that the files will only exist if $DirPath is the current working directory.
 
Also, try printing the outcome of using -M to see what values you are getting back. That'll give you a better indication of what comparisons you need to make.
 
either chdir into that dir or do a -M "$DirPath/$file"

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top