CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:
If statement / -M help



Apr 8, 2001, 4:51 PM

Post #1 of 3 (244 views)
If statement / -M help Can't Post

Trying to write an if statement that will rename files over x days old to another location. If I take out the "if" part in this code it renames all files. If I leave the if statement it doesn't work. So the problem is in the if statement, I just don't know what I am doing wrong in it. Any suggestions would be appreciated.

opendir (MESSAGES, $archivedir);
@messages = readdir (MESSAGES);
foreach $message (@messages) {

if ((-M "$archivedir/$message") >= 1) {
rename ("$archivedir/$message", "$archive/$message");
closedir (MESSAGES);


Apr 10, 2001, 6:37 AM

Post #2 of 3 (224 views)
Re: If statement / -M help [In reply to] Can't Post

Hello HR,

here's what i came thru about your problem:

$filemask  = "*txt"; 
$maxdays = 30;
$backupdir = "../bak/"

for (glob $filemask) {
if ( -f ) {
print "$_ is ", int(-M $_)," day(s) old";
print " renamed to $backupdir/$_" if ((-M $_) > $maxdays);
print "\n";

you'll just need to replace print " renamed to $backupdir/$_" with rename $_,"$backupdir/$_" or somthing else that fits your needs... btw, also notice that i added the if (-f) because with it could start to rename directories... (yeah, i tested Crazy)

i hope this helps,
see you

; -----
Real Programmers dont need class,
They just have style...


Apr 10, 2001, 9:38 AM

Post #3 of 3 (221 views)
Re: If statement / -M help [In reply to] Can't Post

I would replace
if ((-M $_) > $maxdays);
if (int(-M $_) > $maxdays);
because -M returns fractions and 30.3427 is greater than 30 and resulting rename will probably confuse you.

Jean Spector
QA Engineer @ Extent Technologies, Ltd.


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives