Suppose I have a filehandle $fh
. I can check its existence with -e $fh
or its file size with -s $fh
or a slew of additional information about the file. How can I get its last modified time stamp?
You need the stat call, and the file name:
my $last_mod_time = (stat ($file))[9];
Perl also has a different version:
my $last_mod_time = -M $file;
but that value is relative to when the program started. This is useful for things like sorting, but you probably want the first version.
You could use stat() or the File::Stat module.
perldoc -f stat
I think you're looking for the stat
function (perldoc -f stat
)
In particular, the 9th field (10th, index #9) of the returned list is the last modify time of the file in seconds since the epoch.
So:
my $last_modified = (stat($fh))[9];
You need the stat call, and the file name:
my $last_mod_time = (stat ($file))[9];
Perl also has a different version:
my $last_mod_time = -M $file;
but that value is relative to when the program started. This is useful for things like sorting, but you probably want the first version.
This is very old thread, but I tried using the solution and could not get the information out of File::stat. (Perl 5.10.1)
I had to do the following:
my $f_stats = stat($fh);
my $timestamp_mod = localtime($f_stats->mtime);
print "MOD_TIME = $timestamp_mod \n";
Just thought I share in case anyone else had the same trouble.
You need the stat call, and the file name:
my $last_mod_time = (stat ($file))[9];
Perl also has a different version:
my $last_mod_time = -M $file;
but that value is relative to when the program started. This is useful for things like sorting, but you probably want the first version.
Use the builtin stat function. Or more specifically:
my $modtime = (stat($fh))[9]
You could use stat() or the File::Stat module.
perldoc -f stat
I think you're looking for the stat
function (perldoc -f stat
)
In particular, the 9th field (10th, index #9) of the returned list is the last modify time of the file in seconds since the epoch.
So:
my $last_modified = (stat($fh))[9];
If you're just comparing two files to see which is newer then -C
should work:
if (-C "file1.txt" > -C "file2.txt") {
{
/* Update */
}
There's also -M
, but I don't think it's what you want. Luckily, it's almost impossible to search for documentation on these file operators via Google.
I think you're looking for the stat
function (perldoc -f stat
)
In particular, the 9th field (10th, index #9) of the returned list is the last modify time of the file in seconds since the epoch.
So:
my $last_modified = (stat($fh))[9];
Use the builtin stat function. Or more specifically:
my $modtime = (stat($fh))[9]
You could use stat() or the File::Stat module.
perldoc -f stat
my @array = stat($filehandle);
The modification time is stored in Unix format in $array[9].
Or explicitly:
my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size,
$atime, $mtime, $ctime, $blksize, $blocks) = stat($filepath);
0 dev Device number of filesystem
1 ino inode number
2 mode File mode (type and permissions)
3 nlink Number of (hard) links to the file
4 uid Numeric user ID of file's owner
5 gid Numeric group ID of file's owner
6 rdev The device identifier (special files only)
7 size Total size of file, in bytes
8 atime Last access time in seconds since the epoch
9 mtime Last modify time in seconds since the epoch
10 ctime inode change time in seconds since the epoch
11 blksize Preferred block size for file system I/O
12 blocks Actual number of blocks allocated
The epoch was at 00:00 January 1, 1970 GMT.
More information is in stat.
You need the stat call, and the file name:
my $last_mod_time = (stat ($file))[9];
Perl also has a different version:
my $last_mod_time = -M $file;
but that value is relative to when the program started. This is useful for things like sorting, but you probably want the first version.
On my FreeBSD system, stat
just returns a bless.
$VAR1 = bless( [
102,
8,
33188,
1,
0,
0,
661,
276,
1372816636,
1372755222,
1372755233,
32768,
8
], 'File::stat' );
You need to extract mtime
like this:
my @ABC = (stat($my_file));
print "-----------$ABC['File::stat'][9] ------------------------\n";
or
print "-----------$ABC[0][9] ------------------------\n";
You could use stat() or the File::Stat module.
perldoc -f stat
This is very old thread, but I tried using the solution and could not get the information out of File::stat. (Perl 5.10.1)
I had to do the following:
my $f_stats = stat($fh);
my $timestamp_mod = localtime($f_stats->mtime);
print "MOD_TIME = $timestamp_mod \n";
Just thought I share in case anyone else had the same trouble.
my @array = stat($filehandle);
The modification time is stored in Unix format in $array[9].
Or explicitly:
my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size,
$atime, $mtime, $ctime, $blksize, $blocks) = stat($filepath);
0 dev Device number of filesystem
1 ino inode number
2 mode File mode (type and permissions)
3 nlink Number of (hard) links to the file
4 uid Numeric user ID of file's owner
5 gid Numeric group ID of file's owner
6 rdev The device identifier (special files only)
7 size Total size of file, in bytes
8 atime Last access time in seconds since the epoch
9 mtime Last modify time in seconds since the epoch
10 ctime inode change time in seconds since the epoch
11 blksize Preferred block size for file system I/O
12 blocks Actual number of blocks allocated
The epoch was at 00:00 January 1, 1970 GMT.
More information is in stat.
If you're just comparing two files to see which is newer then -C
should work:
if (-C "file1.txt" > -C "file2.txt") {
{
/* Update */
}
There's also -M
, but I don't think it's what you want. Luckily, it's almost impossible to search for documentation on these file operators via Google.
On my FreeBSD system, stat
just returns a bless.
$VAR1 = bless( [
102,
8,
33188,
1,
0,
0,
661,
276,
1372816636,
1372755222,
1372755233,
32768,
8
], 'File::stat' );
You need to extract mtime
like this:
my @ABC = (stat($my_file));
print "-----------$ABC['File::stat'][9] ------------------------\n";
or
print "-----------$ABC[0][9] ------------------------\n";
Use the builtin stat function. Or more specifically:
my $modtime = (stat($fh))[9]
I think you're looking for the stat
function (perldoc -f stat
)
In particular, the 9th field (10th, index #9) of the returned list is the last modify time of the file in seconds since the epoch.
So:
my $last_modified = (stat($fh))[9];
my @array = stat($filehandle);
The modification time is stored in Unix format in $array[9].
Or explicitly:
my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size,
$atime, $mtime, $ctime, $blksize, $blocks) = stat($filepath);
0 dev Device number of filesystem
1 ino inode number
2 mode File mode (type and permissions)
3 nlink Number of (hard) links to the file
4 uid Numeric user ID of file's owner
5 gid Numeric group ID of file's owner
6 rdev The device identifier (special files only)
7 size Total size of file, in bytes
8 atime Last access time in seconds since the epoch
9 mtime Last modify time in seconds since the epoch
10 ctime inode change time in seconds since the epoch
11 blksize Preferred block size for file system I/O
12 blocks Actual number of blocks allocated
The epoch was at 00:00 January 1, 1970 GMT.
More information is in stat.
Source: Stackoverflow.com