faqts : Computers : Programming : Languages : Perl : Common Problems : File Handling

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

18 of 21 people (86%) answered Yes
Recently 9 of 10 people (90%) answered Yes

Entry

How can I write a perl program which can move files in a directory which are more than one day old?

Aug 31st, 2001 09:40
Tony Crosby, Harish Patil,


More than a day Old? I would suspect you mean since the last time they 
were modified. If not, they you may want to use a timestamp with the 
creation of your files, but this is much simpler.
-----------------------------------------------------------------------
#!/usr/bin/perl
print "Content-type: text/html\n\n";
use Time::Local;
@GmTime=gmtime();
$current_time=timegm(@GmTime);
$current_time -= 86400;
$dir = "/path/to/dir";
# ^ path to the directory to anylize
$relocation_dir = "/path/to/new_dir";
# ^ path to the directory in which the old files will go
opendir (DIR, $dir);
@files =
sort
grep { -f "$dir/$_" }
readdir (DIR);
closedir (DIR);
foreach $file (@files)
{
@stats = stat("$dir/$file");
if ($current_time > $stats[8])
{
rename "$dir/file", "$relocation_dir/$file";
# was modified more than a day ago, move it
}
}
 Tony Crosby