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

changing date of file to seconds since 1970 1

Status
Not open for further replies.

ironpawz

IS-IT--Management
Oct 8, 2002
44
0
0
NZ

I am reading the date a file was last modified. I have set this as a variable. I then run a process that modifies that date/time and I want to change it back.

The date format I am getting is 18/10/2002 11:25:00 (by reading the properties of a word file). I am planing to us utime to reset this date/time once I have changed the company name of the document.

The question would be are there better ways of getting the accessed date of the file already in this format?

use File::stat; seems promising?

Or how can I convert the date/time in the above format to the seconds after 1970 that utime required?

Thanks
 
You can use perls builtin [tt]stat()[/tt] function.

[tt]$mtime = (stat($filename))[9];[/tt] will set $mtime to the last modification time as number of seconds since the epoch (exactly what you're looking for.)
 

Yes thanks for this I did find it and it looks really promising but it dont work! I am using (excuse the mess I am trying to tweek it).


use File::stat;
my $file = "c:\\this.doc";
($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blks)= stat($file);

($sec, $min, $hour, $mday, $mon, $year, $wday, $yday, $isdst)= localtime($mtime);
$year= 1900 + $year;


print $dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blks;

print "mtime (the last modify time since the epoch): $mtime\n";
print "Applying mtime to localcaltime time we get: ";
printf(" %d-%d-%d\n",$mon+1,$mday,$year);

All I get is

C:\>utime.pl
File::stat=ARRAY(0x888728)mtime (the last modify time since the epoch):
Applying mtime to localcaltime time we get: 1-1-1970

I tried this on two machines. One is running perl 5.8 and both did the same thing. Still the weekend is here I might take a look later otherwise monday.

you get the star as the answer is perfect just need to get it to work!

I also tried your code like this

use File::stat;
my $file = "c:\\this.doc";
$mtime = (stat($file))[9];
print $mtime;

C:\>perl -w utime.pl
Use of uninitialized value at utime.pl line 4. ????
looks like a Monday prob Thanks again!


 
If you [tt]use File::stat;[/tt] then [tt]stat()[/tt] returns a blessed reference instead of an array. You would do:

[tt]use File::stat;
my $file = "c:\\this.doc";
my $st = stat($file) or die "No $file";
my $mtime = $st->mtime();
print $mtime;
[/tt]

if you didn't do [tt]use File::stat[/tt] you could do:

[tt]my $file = "C:\\this.doc";
my $mtime = (stat($file))[9];
print $mtime;
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top