Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Finding out the date a file was last created, or modified 1

Status
Not open for further replies.

mincefish

Programmer
Sep 27, 2001
74
GB
I need to run through a load of directories, and end up with a list of files that have not been used for the past 3 months. The files do have a date within the title, but as all the file names have been entered manually, they vary:
Code:
title-dd - mm - yy
title-dd-mm-yy
title-dd-mm-yy
title dd - mm - yy
title dd-mm-yy
titledd-mm-yy[\code]

The different formats go on, believe me!
I have created a perl5 script to deal with some of this formatting, but it is not ideal, and there are some files that get missed out.
What would make it one hundred times easier, would be able to get hold of the date created or date last modified part of the file - then I could easily get rid of files I didn't want, without having to deal with essentially incompetent file naming ;-)

I hope I have provided you with all the information you need to solve this head scratcher....

Cheers

the mince-fish...
 
Hi - I wrote this to remove logs which were created over 90 days previously - you should be able to modify it to do what you want

# set Time in days to move logs #####
$Days= 90; #
#####################################
$filedir="$NetDrive/log/$Family/$Dirpass/";
$filedir1="$NetDrive/log/$Family/$Dirfail/";
$countStop=300;

print "Checking for log over $Days Day(s)....\nPlease Wait ..\n";

opendir DIR, $filedir;
my @array = readdir DIR;
closedir DIR;

opendir DIR1, $filedir1;
my @array1 = readdir DIR1;
closedir DIR1;
$countStart=1;
foreach $files(@array)
{
$file="$filedir$files";

if (-T $file) # check to see if a text file?
{
$min = 1;
checkFile($min, $file);
}
else
{
next;
}
}

print "\nChecking failed directories ....\nPlease Wait...\n";
$countStart=1;
foreach $files(@array1)
{
$file="$filedir1$files";

if (-T $file) # check to see if a text file?
{
$min = 1;
checkFile($min, $file);
}
else
{
next;
}
}

sub checkFile
{
my ($min, $file) = @_;
$min *= (60*24*$Days);
my $t = time;
my @tmp = stat($file);
if($tmp[9] < ($t - $min*60))
{
print &quot;File over $Days Day(s) old Moving $file to Archive !\n&quot;;
move(&quot;$file&quot;,&quot;$NetDrive/qmds&quot;);
#unlink($file);
if ($countStart > $countStop){exit}
$countStart++;
return 1;
}
return 0;
}

 
So just to clarify, you can use the Perl &quot;stat&quot; function(see that in johnv20's checkFile subroutine) to find out what attributes a file has. One of those attributes is the last modified date. You can learn about the &quot;stat&quot; function by doing

perldoc -f stat

at a command prompt.

HTH. Hardy Merrill
Mission Critical Linux, Inc.
 
Thank you both so much. I've been searching in perldoc for something to do that, but when you are simply looking for a function to do something, and not the syntax, it can be time-consuming.

The Mince-fish :)
 
mincefish, not sure if you know this already, but you can search the perldocs by using the &quot;-q&quot; operator - if I wanted to search for &quot;file&quot;, I could do

perldoc -q file

I just did that, and found *LOTS* of stuff, and then I searched the results using the &quot;/&quot;(slash) operator looking for &quot;stat&quot;, and found this section:

How do I get a file's timestamp in perl?

If you want to retrieve the time at which the file
was last read, written, or had its meta-data
(owner, etc) changed, you use the -M, -A, or -C
filetest operations as documented in the perlfunc
manpage. These retrieve the age of the file (mea­
sured against the start-time of your program) in
days as a floating point number. To retrieve the
&quot;raw&quot; time in seconds since the epoch, you would
call the stat function, then use localtime(),
gmtime(), or POSIX:\fIs0:strftime() to convert
this into human-readable form.

Here's an example:

$write_secs = (stat($file))[9];
printf &quot;file %s updated at %s\n&quot;, $file,
scalar localtime($write_secs);

so it can be done - it just takes time. You'll get better at it the more you do it :)

HTH. Hardy Merrill
Mission Critical Linux, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top