Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Oh wise perl monks please help! I have the following code, but the utime() function doesn't seem to be writing back the modified time correctly, what am I doing wrong?
$dir="c:/corrupt"; use File::Find; find(\&dofile, $dir); sub dofile{ if($File::Find::name =~ /.txt/){ open(FILE, "$File::Find::name") or die "$!"; @lines=<FILE>; close(FILE); #remove the offending space on 1st line if(substr($lines[0], 3, 1) eq " "){ #get mtime $ENV{TZ}="GMT"; $savetime=(stat("$File::Find::name"))[9]; print "corrupt line 1, repairing $File::Find::nam +e..."; $lines[0]=substr($lines[0], 0, 3, "") . substr($l +ines[0], 1); unlink("$File::Find::name") or die "$!"; open (FILE, ">>$File::Find::name") or die "$!"; print FILE @lines; #write back old mtime utime($savetime, $savetime, "$File::Find::name") +or die "$!"; close(FILE); print "done\n"; } }}

Replies are listed 'Best First'.
•Re: alter file without changing mtime?
by merlyn (Sage) on Aug 14, 2003 at 13:43 UTC
      nice one man, worked perfectly!