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:
Script to delete only those files on a path whose accessed date is more than 7 days

 



perl_learner
New User

Aug 13, 2012, 2:36 PM

Post #1 of 3 (1249 views)
Script to delete only those files on a path whose accessed date is more than 7 days Can't Post

Hi Friends,

I am new to perl. I am trying to work on a perl script which will delete only those files on a particular path which are older than 7 days in terms of accessed date of the files. However, it seems not working. But, when i put a condition "M" it does work for a modified date condition. Below is the code. Please suggest what should I do. Thanks for your help and suggestion.


Code
#!/usr/local/bin/perl-w  
my $username = getlogin;
my $userpath = "C:/Users/$username/AppData/Local/Temp/Data";
print $username;
opendir (DIR, "$userpath") ||
die ("Unable to open directory");
@files=readdir(DIR);
closedir(DIR);
foreach $file (@files) {
if (-A "$userpath/$file" > 7) {
print ("$file\n");
print $userpath."/".$file;
unlink $userpath."/".$file;
print "File deleted successfully\n";
}
}



Laurent_R
Veteran / Moderator

Aug 14, 2012, 4:59 AM

Post #2 of 3 (1230 views)
Re: [perl_learner] Script to delete only those files on a path whose accessed date is more than 7 days [In reply to] Can't Post

I though that, maybe, the last access date is not maintained under Windows (I am usually working on other operating systems, I don't know Windows well, I just use it as a work station) and therefore the -A file test does not return something sensible.

But I've just tried under Windows:


Code
perl -e "print -A qq/foo.bar/"


and it did print 356,825... And by the way if I use -M, I get a much larger number.

So I was wrong in this hypothesis.

If exactly the same code work with -M and not with -A, that is quite strange. Does it print anything?


FishMonger
Veteran / Moderator

Aug 14, 2012, 6:59 AM

Post #3 of 3 (1221 views)
Re: [Laurent_R] Script to delete only those files on a path whose accessed date is more than 7 days [In reply to] Can't Post

Windows does not maintain/update the atime timestamp. On Windows the -A aka atime file test returns the same as the -C aka ctime.

 
 


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

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