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!

Change the last modified date of a file

Status
Not open for further replies.

Frickydel

Programmer
Sep 10, 2002
5
FR
Hello,
Is the a way to change informations about a file like the last modified date??
look like -M $myfile and stat($myfile) are read commands only
Please help a poor french!

Thanks
 
9.1. Getting and Setting Timestamps
Problem
You need to retrieve or alter when a file was last modified (written or changed) or accessed (read).

Solution
Use stat to get those times and utime to set them. Both functions are built into Perl:

($READTIME, $WRITETIME) = (stat($filename))[8,9];

utime($NEWREADTIME, $NEWWRITETIME, $filename);
Discussion
As explained in the Introduction, three different times are associated with an inode in the traditional Unix filesystem. Of these, any user can set the atime and mtime with utime, assuming the user has write access to the parent directory of the file. There is effectively no way to change the ctime. This example shows how to call utime:

$SECONDS_PER_DAY = 60 * 60 * 24;
($atime, $mtime) = (stat($file))[8,9];
$atime -= 7 * $SECONDS_PER_DAY;
$mtime -= 7 * $SECONDS_PER_DAY;

utime($atime, $mtime, $file)
or die "couldn't backdate $file by a week w/ utime: $!";
You must call utime with both atime and mtime values. If you only want to change one, you must call stat first to get the other:

$mtime = (stat $file)[9];
utime(time, $mtime, $file);
This is easier to understand if you use File::stat:

use File::stat;
utime(time, stat($file)->mtime, $file);
Use utime to make it appear as though you never touched a file at all (beyond its ctime being updated). For example, to edit a file, use the program in Example 9.1.

Example 9.1: uvi
#!/usr/bin/perl -w
# uvi - vi a file without changing its access times

$file = shift or die "usage: uvi filename\n";
($atime, $mtime) = (stat($file))[8,9];
system($ENV{EDITOR} || "vi", $file);
utime($atime, $mtime, $file)
or die "couldn't restore $file to orig times: $!";
See Also
The stat and utime functions in perlfunc (1) and in Chapter 3 of Programming Perl; the standard File::stat module (also in Chapter 7 of Programming Perl; your system's utime (3) manpage


Got this from my Programming Perl book.
Thanks
Tricia
yorkeylady@earthlink.net
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top