CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
setting timestamps on files utime or alternatives

 



ironpaw
Novice

Jan 22, 2003, 7:53 PM

Post #1 of 12 (1090 views)
setting timestamps on files utime or alternatives Can't Post

lo ppl. I have this script for reading a csv like so:
c:\thisdir\thatone\somedoc.xls , 102324455 (unix date stamp)
and taking those values and setting the last changed date on the file in question. I am using utime and it works IF i own the file (even though I have rights to the files). I understand this is a limitation of utime so fair enuff. Is there another tool/method of setting files back to their original time? I need to set the last changed date on all files down a dir (and subs) that I have write access to but do not always own.

use Win32::OLE;
use File::stat;

open(CSV, "c:/temp/doctime.txt"); #csv of filename , date (in seconds since epoc)
@title = <CSV>;

for ($i = 0; $i < scalar(@title); $i++)
{
($file1[$i], $date[$i] $blank[$i]) = split(",", $title[$i]);
}

for ($a = 0; $a < scalar(@file1); $a++)
{

$access=$date[$a];
$filez = $file1[$a];

print "File name # $filez\n";
print "Previous date # $access\n";
utime $access, $access, $filez || die print "couldn not set utime for
+$filez\n";

}
# end of script
This works great loyally skipping any file not owned by me (which I don't want it to as I own bugger all of em). I am running it from Win2k and the files are on Unix/Samba but the same thing happens locally. Any advice ideas would be great. Smile I will even happly pass it off to a dos tool if I could find one that allows long file names Laugh.
pawz
###############
All Life is Kaos
###############


davorg
Thaumaturge / Moderator

Jan 23, 2003, 1:19 AM

Post #2 of 12 (1074 views)
Re: [ironpaw] setting timestamps on files utime or alternatives [In reply to] Can't Post

I'm pretty sure that it's not a problem with "utime", but a perfectly reasonable restriction put in place by the operating system. You can only alter files that you have permissions to alters.

What are the Unix file permissions on the files. And on the directory that they live in?

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


ironpaw
Novice

Jan 23, 2003, 11:40 AM

Post #3 of 12 (1070 views)
Re: [davorg] setting timestamps on files utime or alternatives [In reply to] Can't Post

I thought that was the case myself but further down is an extract from utime man which mentions this restriction if a file has a non numl date you must own it.

Here are the unix file perms. The cyp group has rw (I am a member). Samba permissions are rw also. I can open edit and save a file at which point it becomes owned by me and I can then use utime on it. (note the file owned by me btayl00a I can change with utime).

drwxrwx--- 2 creyn00z cyp 1024 Jan 23 13:41 .
drwxrwxrwx 11 root cyp 512 Jan 22 16:14 ..
-rw-rw---- 1 creyn00z cyp 18432 Jan 22 16:17 Application Server Review.xls
-rw-rw---- 1 creyn00z cyp 75776 Jan 22 16:17 CYF-APP-SVR-ISATION.doc
-rw-rw---- 1 creyn00z cyp 20480 Jan 22 16:17 CYF-APPLICATION-SERVERS.doc
-rw-rw---- 1 creyn00z cyp 23552 Jan 22 16:17 CYF_CR-cytys005-pcms.xls
-rw-rw---- 1 btayl00a cyp 23552 Sep 16 10:39 MSP_CR_New diensions server.xls


Directory permissions

drwxrwx--- 2 creyn00z cyp 1024 Jan 23 13:41 cr

From utime/MAN:
If times is not NULL, times is interpreted as a pointer to a utimbuf structure (defined in utime.h) and the access and modification times are set to the values contained in the designated structure. Only the owner of the file may use utime this way.
http://www.mcsr.olemiss.edu/cgi-bin/man-cgi?utime+2
So apparently there is a restriction by owner if the file has a date already. This leads to two questions, can I remove that date another way and then set it? And is there another way to set it? A dos tool allowing long file names? Another perl operation? Can touch do it (is that a perl function as well as unix).

Thanks for your reply. I'll be looking into this today and checking here so I'll post anything updates I find also. I'm in NZ so your probably alseep <G>.
###############
All Life is Kaos
###############


davorg
Thaumaturge / Moderator

Jan 24, 2003, 2:01 AM

Post #4 of 12 (1063 views)
Re: [ironpaw] setting timestamps on files utime or alternatives [In reply to] Can't Post

That's the Unix "utime" man page. You should really be looking at "perldoc -f utime" instead.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


ironpaw
Novice

Jan 26, 2003, 11:50 AM

Post #5 of 12 (1056 views)
Re: [davorg] setting timestamps on files utime or alternatives [In reply to] Can't Post

Thanks. I searched perldoc.com and get very little but:

"Note that utime() currently doesn't work correctly with Win95/NT ports. A bug has been reported. Check it carefully before using utime() on those platforms."

Not very helpful but it is information I guess.

"Only the modification time is updated. (Mac OS, VMS, RISC OS)

May not behave as expected. Behavior depends on the C runtime library's implementation of utime(), and the filesystem being used. The FAT filesystem typically does not support an "access time" field, and it may limit timestamps to a granularity of two seconds. (Win32)"

Also not a great deal but Samba is seen by windows to be fat isn't it? I'm not fully sure. Either way I am using utime in a very standard way, it does set the date for a file I own and does not for a file I don't (and yes I have access to all these files).

I think it is safe to find self evident that perl utime does what I read about unix utime being it only works for files I own. I am now writing the script so it can be run directly on the unix server (verses from my win box) as root it may change em (root owns everything right).

I would like to know what the "reported bugs in the NT/95 version" actually are but I don't suppose it really matters at this point.

What I am really interested in is if there are any alternatives (running things on the unix side means more ppl are involved and more paperwork etc etc).

Thanks for yr suggestions though perldoc is good (if a little uninformative in this case).

Paws
###############
All Life is Kaos
###############


ironpaw
Novice

Feb 4, 2003, 7:07 PM

Post #6 of 12 (1034 views)
Re: [ironpaw] setting timestamps on files utime or alternatives [In reply to] Can't Post

The problem is that utime does not set dates for files you do not own (even if you have write access). The answer in my case was to run the script on the unix box as root who owns all files. Unix does not like spaces in files so the script got modified. Utime does not work with:

utime ($variablefordate, $variablefordate ,"$FullPathAndFileName");

but does work with (keep in mind these file names and paths contain spaces)

utime ($variablefordate, $variablefordate ,"$JustFileName");
# this works if your already in the correct directory.. chdir("/path/with spaces/ok);

Here is my final script

#!/usr/bin/perl -w

open(CSV, "/tmp/doctime.txt");
@title = <CSV>;

for ($i = 0; $i < scalar(@title); $i++)

{
($dironly[$i], $filename[$i], $date[$i]) = split(",", $title[$i]);
}

for ($a = 0; $a < scalar(@filename); $a++)
{
$access= $date[$a];
$file = $filename[$a];
$dir = $dironly[$a];

chdir("$dir");

print "$dir $file $access\n";
print utime ($access, $access, "$file"), "\n";
print "\n\n";
}
###############
All Life is Kaos
###############


davorg
Thaumaturge / Moderator

Feb 5, 2003, 1:58 AM

Post #7 of 12 (1030 views)
Re: [ironpaw] setting timestamps on files utime or alternatives [In reply to] Can't Post

Can I suggest a few changes to that code to eliminate unnecessary loops, variables and memory usage - and to introduce some good coding practices.

Code
#!/usr/bin/perl -w 

use strict;
use Cwd;

my $root = cwd;

open(CSV, '/tmp/doctime.txt') or die $!;

while (<CSV>) {
chomp;
my ($dir, $file, $date) = split /,/;

chdir($dir) or die "Can't chdir to $dir";

print "$dir $file $date\n";
print utime ($access, $access, $file), "\n";
print "\n\n";

chdir $root or die "Can't chdir to $root";
}


--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


ironpaw
Novice

Feb 6, 2003, 11:57 AM

Post #8 of 12 (1024 views)
Re: [davorg] setting timestamps on files utime or alternatives [In reply to] Can't Post

Thanks Davorg,

print utime ($access, $access, "$file"), "\n";

It will need those "" to deal with the names with spaces. cwd is change working directory isn't it? Wouldn't chdir $root be like saying change directory to change local directory?

I'll have a read and a play. Does it even need that last line? I take it this would run a lot faster also. Use Strick time I looked up the benefits of that also.

Nice one thank you, it's greate to make something work (anyway you can) then look at how it would look using good practices and learn that too ;)
###############
All Life is Kaos
###############


davorg
Thaumaturge / Moderator

Feb 6, 2003, 1:17 PM

Post #9 of 12 (1021 views)
Re: [ironpaw] setting timestamps on files utime or alternatives [In reply to] Can't Post


In Reply To
print utime ($access, $access, "$file"), "\n";

It will need those "" to deal with the names with spaces.

No it won't. Did you try it?


In Reply To
cwd is change working directory isn't it?

No. "cwd" stands for "current working directory". It returns the current directory - i.e. the one that your process starts in.


In Reply To
Wouldn't chdir $root be like saying change directory to change local directory?

I make a note of the current directory before starting the loop. Each time round the loop I change into the necessary directory, do the work and thne go back you where I started.


In Reply To
I'll have a read and a play. Does it even need that last line?

Maybe not. It depends on how your directories are structured in the file. If they are relative directories then you'll need to go back to your starting point before you can do the next "chdir". If they are absolute directories then it's not necessary, but I'd still do it as it just seems tidier to me to exit the loop with the process in the same condition as it was when you entered it.

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


ironpaw
Novice

Feb 6, 2003, 1:45 PM

Post #10 of 12 (1020 views)
Re: [davorg] setting timestamps on files utime or alternatives [In reply to] Can't Post

Your script runs nicely. I did go have a read about "use Cwd;" so thanks for that. It will come in handy for other things I bet. I also tidied some other code where I use a lot of "for" where I should be using "while" which is obviously much more efficent in some cases.

from you: No it won't. Did you try it? RE: using ""

Yes extensively. If the filename contains spaces it just misses it out unless you enclose it. I am saying this from trial and error not knowledge of perl or unix so it only worked that way for me. Lots of our files contain spaces as do the directories. Utime is strange I have had plenty of discussion on how it should work but in the end it is how it does work verses how it "should" work that counts ;).

I might try tidy the last few scripts and post them to see if they also need attention seeing as people such as yourself seem to enjoy streamlining (I know I do in areas I know better).

Thanks again.
###############
All Life is Kaos
###############


davorg
Thaumaturge / Moderator

Feb 6, 2003, 2:37 PM

Post #11 of 12 (1019 views)
Re: [ironpaw] setting timestamps on files utime or alternatives [In reply to] Can't Post


In Reply To
from you: No it won't. Did you try it? RE: using ""

Yes extensively. If the filename contains spaces it just misses it out unless you enclose it. I am saying this from trial and error not knowledge of perl or unix so it only worked that way for me. Lots of our files contain spaces as do the directories. Utime is strange I have had plenty of discussion on how it should work but in the end it is how it does work verses how it "should" work that counts ;).

That's really strange. There's no way that the presence or absence of quotes should effect that. Here's my simple test:

Code
$ touch "a file" 
$ ls -l a*
-rw-rw-r-- 1 dave dave 0 Feb 6 22:41 a file
$ perl -e '$file = "a file"; utime 1, 1, $file'
$ ls -l a*
-rw-rw-r-- 1 dave dave 0 Jan 1 1970 a file

That seems to be exactly what I expected. What happens when you try it?

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


ironpaw
Novice

Feb 6, 2003, 2:52 PM

Post #12 of 12 (1017 views)
Re: [davorg] setting timestamps on files utime or alternatives [In reply to] Can't Post

oh fair enough it worked. So I changed the original script back and it still worked as you said. When I was doing my original messy one I was changing all sorts of minor things to make it work and that was one of the factors. Originally I was feeding utime the whole path and filename and no matter what it did not do the ones with spaces (hench changing dirs and using just the name).

Thanks for the correction. Perhaps it is partly the general better operating of your script. Unix and perl are obviously happier with spaces than I first thought (which is a pleaseant surprise).

Keep me honest ;)
###############
All Life is Kaos
###############

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives