@tm = localtime;
$mytime = sprintf("%d%02d%02d%02d%02d%02d", $tm[5]+1900, $tm[4]+1, $tm
+[3], $tm[2], $tm[1], $tm[0])
rename ($filea, "$filea$mytime") || die "Couldn't rename file: $!";
will work. If you want just the first couple of letters, use
@tm = localtime;
$mytime = sprintf("%d%02d%02d%02d%02d%02d", $tm[5]+1900, $tm[4]+1, $tm
+[3], $tm[2], $tm[1], $tm[0])
rename ($filea, substr($filea,0,3).$mytime) || die "Couldn't rename fi
+le: $!";
Here is how this works. If you call localtime with an array, the array is filed with the date and time. Element five of the array is the year, four is the month, three is the day, two the hour, one the minute, and zero the seconds. You need to add 1900 to the year, or if you want a two digit year, you need to subtract 100 (if the year is 20xx). The month you need to add 1 to (it's made to easly reference an array). The sprintf formats the string. The rename renames a file. The substr command extracts a part of a string (in this case the first three letters). Finaly, the '.' operator concatenats two strings. | [reply] [d/l] [select] |
#!/usr/bin/perl -w
use strict;
use File::Copy;
my $current_filename = "foo";
my $new_filename = "foo" . localtime;
move($current_filename, $new_filename)
or die "Couldn't move $current_filename to $new_filename: $!\n
+";
Be aware that this code creates a file with spaces in the filename, which is generally considered not a nice thing (tm).
hope this helps
davis | [reply] [d/l] |
Have a look at the excellent File::Copy.
Update: And, of course, in most cases the
rename builtin will do just fine. (D'oh!)
rename won't work across filesystem boundaries,
which probably doesn't concern you much, but you never
know. (Thanks tye and petral for pointing out my
omission.)
--
:wq
| [reply] |
Since I don't know whether or not you have control over the application that is doing the FTP or not, this may or may not be valid...
But if you are coding the ftp portion of the application, you could change the file name during the transfer instead of after the fact.
An example using Net::FTP...
use Net::FTP;
my $mytime = localtime;
my $file = "filea"
# Set up the FTP
my $ftp = new Net::FTP;
$ftp->login('name', 'password');
# If you're putting the file somewhere...
$ftp->put($file, "$file$mytime");
# Or if you're getting the file from somewhere...
$ftp->get($file, "$file$mytime");
$ftp->close();
Net::FTP allows you to specify a new name and location for a file when you are getting or putting a file - I would assume other FTP methods/modules would do the same.
Rich36
There's more than one way to screw it up...
| [reply] [d/l] |