There are a number of different ways you can determine how old a file is
or when it was last modified. However, trying to find these examples can
be difficult at best and if you don't use them all the time, you quickly
forget how to do it and have to relearn it each time.
This utility will give you the age of a specified file in seconds, minutes,
hours, days, weeks, years. It will also return the date the file was
created (placed on the server) and the date it was last modified. The first
six values are returned in decimal format but you can also retrieve them in
integer format (no decimals, rounded to the lowest whole value).
For our example, we will use two files. The file "old.txt" was placed on the
server one year, three days, four hours and twelve minutes ago. The second
file "new.txt" was placed on the server precisely 36 hrs ago. First I will
show you what age values the program returns based on the above examples.
| |
Old File |
New File |
| Use Integer |
Yes |
No |
Yes |
No |
| Seconds |
31810320 |
31810320 |
129600 |
129600 |
| Minutes |
530172 |
530172 |
2160 |
2160 |
| Hours |
8836 |
8836.2 |
36 |
36 |
| Days |
368 |
368.175 |
1 |
1.5 |
| Weeks |
52 |
52.596428 |
0 |
0.2142857 |
| Years |
1 |
1.008698 |
0 |
0.0041095 |
| Created |
Wednesday, September 21, 22:12:46, 1998 |
Monday, September 24, 12:45:13, 1999 |
Last Modified |
Saturday, June 19, 08:12:12, 1999 |
Monday, September 24, 12:57:13, 1999 |
To call the script you must pass it two parameters. The first is the format
that you want the age returned in and the second is the absolute path to
the file you are querying.
- Formats
- The fileage() subroutine accepts nine (9) different formats. These formats
are:
- sec - This returns the age of the file in seconds.
- min - This returns the age of the file in minutes.
- hr - This returns the age of the file in hours.
- day - This returns the age of the file in days.
- wk - This returns the age of the file in weeks.
- yr - This returns the age of the file in years.
- date - This returns the date the file was created.
- mod - This returns the date the file was last modified.
- '' or 0 - Passing a null value as the format parameter will return a
list containing all of the above values in the following order:
($seconds, $minutes, $hours, $days, $weeks, $years, $date_created, $date_modified) = filedate('', $foo);
To use this utility, load it with your other script that is calling it and CHMOD
it to 755. The other script must have a require statement before the subroutine
fileage() is called. The require statement looks like this:
require 'fileage.pl';
The first parameter you pass is the format and the second parameter you pass
is the absolute path to the file being tested. If you want to use the integer function
so that you are not getting any decimals, then add a third parameter of '1'.
Here are some example calls:
- $age_in_seconds = fileage('sec', '/home/name/html/path/to/file.txt');
- $age_in_minutes = fileage('min', $filepath, 1);
- $age_in_hours = fileage('hr', "$Data/$user/$file");
- $age_in_days = fileage('day', "$FILE{$id_num}.txt", 1);
- $age_in_weeks = fileage('wk', $file_array[$b], 1);
- $age_in_years = fileage('yr', "$Root/$Dir/$File");
- $date_created = fileage('date', "$Data/$file");
- $date_modified = fileage('mod', $FilePath);
- @age_array = fileage('', $FilePath);
- @age_array = fileage(0, "$Data/$file");
- @age_array = fileage(0, "$Data/$file", 1);
If you have any difficulty using this script of it's not explained clear
enough, please email me and tell me exactly where you having the problem and
I will try to explain it better in this document.
|