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