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

Change Modified Time for a File.

Status
Not open for further replies.

Milleniumlegend

IS-IT--Management
Dec 16, 2003
135
Could you please let me know how to change the modified times for a read only file. I have a read only file and want the file to retain its read only attributes.

I would like to know any methods that would allow me to
1. Lock the File so that it cannot be accessed by any other process.
2. Change the Attributes to Write so that we can change the modified time on them.
3. Change the modified time.
4. Restore the attributes to Read only.

Could you please provide me some pointers on this.

Thanks
 
If it's Windows, see Win32::File for changing the attributes to read-only and back.

Use Win32API::File::Time to change the file times. Here's a test script I wrote a while back for ya to look at:

Code:
use Win32API::File::Time qw(:win);

my ($atime,$mtime,$ctime) = GetFileTime ('type.txt');
print "atime=$atime\nmtime=$mtime\nctime=$ctime\n\n";

$ctime = time() + 99999999;
$ctime += 99999999;
$ctime += 99999999;
$ctime += 99999999;
$mtime = time() - 99999;
$atime = time() + 99999999;

SetFileTime ('type.txt',$atime,$mtime,$ctime);

That provided the following silly attributes to the file:

Code:
Created: Friday, March 22, 2019, 10:03:17 AM
Modified: Monday, July 17, 2006, 4:10:02 PM
Accessed: Today, September 12, 2006, 10:48:16 PM

As for locking the file, The only for-sure ways I know to do this involve opening a filehandle first. I just did a quick CPAN search and found File::Flock which seems to be what you're looking for. I haven't tested it though.
 
Thanks for the information it does help.
I am behind a corporate firewall and I am trying to download the Win32:File as well as
Win32API::File::Time

All I got was the tar file and there is some files within it but I am not sure what File to place where. I am on a Windows based Sytem and trying to execute all the code on Windows XP or Windows 2000 Server.

Could you please let me know how I can download and install the individual programs for these Modules.

Thanks
 
also the syntax for Win32::File is bit ambiguous.

use Win32::File;
$myfile="C:\\Temp\temp.txt" # want to make this a RW File

SetAttributes($myfile, NORMAL);

This does not seem to work.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top