Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

File Rename

by lostinpearl (Initiate)
on Nov 28, 2001 at 22:27 UTC ( [id://128129]=perlquestion: print w/replies, xml ) Need Help??

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

I need to append the time&date to a file name that is ftp'ed. I thought that after the ftp occured I'd just rename the file using a variable that contains part of the original filename. I need to keep the first 3-4 characters of the original name.
$mytime=localtime; rename filea filea$mytime;
Is this possible?

Replies are listed 'Best First'.
Re: File Rename
by c-era (Curate) on Nov 28, 2001 at 22:35 UTC
    @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.
Re: File Rename
by davis (Vicar) on Nov 28, 2001 at 22:33 UTC
    Use the File::Copy Module like so:
    #!/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
Re: File Rename
by FoxtrotUniform (Prior) on Nov 28, 2001 at 22:32 UTC

    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
Re: File Rename
by Rich36 (Chaplain) on Nov 28, 2001 at 23:25 UTC
    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...

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://128129]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2024-04-25 14:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found