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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Finding the Modification Date on a file

Status
Not open for further replies.

scecilia

Programmer
Jul 8, 2002
35
0
0
US
Hello,

Do you know how can I easily extract the date of a file passed in as a parameter? My file looks like:

-rw-r--r-- 1 cecilia grad 477 Oct 13 19:56 message

And I need to get the "Oct 13 19:56" I tried using the "stat" function on this file which returns a 13 element array (one of them being the File last modify time, etc) as follows:

open(IN, "message") or die "Couldn't open file";
@statistics = stat(IN);

But I need to get the Date actually... Do you know how can I do that?

Thanks!
--Cecilia.
 
celia -- hi

the time field is actually the number of seconds since... some-boring-date-or-other so... the localtime() function will convert that into a date and time for you nicely Mike
________________________________________________________________

"Experience is the comb that Nature gives us, after we are bald."

Is that a haiku?
I never could get the hang
of writing those things.
 
Code:
#!perl
# 
$line = '-rw-r--r--    1 cecilia  grad   477 Oct 13 19:56 message';
@parts = split /\s+/, $line;
print "$parts[5] $parts[6], $parts[7]\n";
'hope this helps

If you are new to Tek-Tips, please use descriptive titles, check the FAQs, and beware the evil typo.
 
Use the
Code:
stat
function
The modified time is the 10th element (index 9).
Code:
  @MyArray = stat($somefile);
  $ModTime = localtime(@MyArray[9]);
  print $ModTime;

Have fun!

Thomas Smith
 
Thank you all for your valuable help. The localtime() did it.

Whe you do inside your script something like:

system("ls -l message")

is it possible to capture the line that is printed to the standard output when the command is executed?

Thanks,
 
You could possibly pipe the information
into a text file, then read it...
(That's how I would do it, since I don't know of a
way to directly access the information from the standard
output...)

Not sure that this was of much help,
but I wish you luck!
 
use backticks instead of system(). this will return everything printed to stdout from the command either as a concatinated string if called in scalar context or as an array of lines if called in list context.
Code:
my @filelist = `ls -l`;

jaa
 
I knew I was going to look stupid...
HaHa
Thomas...
 
Backticks is the easiest solution, but just for the sake of completeness, I'll offer this:

$ls_command = "ls -rltd $file |";
open(LS_COMMAND,"ls -l message |) || die "Can't open the ls command!";

while (<LS_COMMAND>) {
print &quot;Here is a line of ls output: $_&quot;;
}

HTH. Hardy Merrill
 
More about Modified Date...

I used tmasman's routine:

@MyArray = stat($somefile);
$ModTime = localtime(@MyArray[9]);
print $ModTime;

and I all get is the epoch date (Wed Dec 31 19:00:00 1969).

Is my ISP blocking the info or what? There's always a better way...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top