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

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

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

 



HR
Novice

Apr 8, 2001, 4:51 PM

Post #1 of 3 (174 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);



freddo
User

Apr 10, 2001, 6:37 AM

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

Hello HR,

here's what i came thru about your problem:

Code
$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...


Jean
User


Apr 10, 2001, 9:38 AM

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

I would replace
if ((-M $_) > $maxdays);
with
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.
mage@lycosmail.com

 
 


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

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