faqts : Computers : Programming : Languages : Perl

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

8 of 10 people (80%) answered Yes
Recently 8 of 10 people (80%) answered Yes


How do I automatically rename a file's name to its modified timestamp?

Mar 18th, 2002 16:26
Bill Rhodes, Christine Wang,

This should do it:
#!/usr/bin/perl -w
use strict;
use Cwd;
my $filename = $ARGV[0] || cwd . "/foo.txt";
if (my $new_filename = (stat($filename))[9]) {
	print "Renaming '$filename' to '$new_filename'...";
	rename($filename, $new_filename) or die "Couldn't rename 
$filename to $new_filename: $!\n";
} else {
	die "Couldn't access $filename: $!\n";
Note that you could also wrap the bulk of this into its own 
function and call it that way.  You could also then make this into 
a module and call it that way.
(BTW, this is my first post here, so pardon my if that doesn't come 
out right.  I have no idea how the answers here get formatted.)