Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Rename a file

by kidd (Curate)
on Jul 14, 2002 at 03:11 UTC ( #181548=perlquestion: print w/ replies, xml ) Need Help??
kidd has asked for the wisdom of the Perl Monks concerning the following question:

What is the fastes and safest way to rename a file ?

e.g hamburger.txt -> pizza.txt (hungry?)

Comment on Rename a file
Re: Rename a file
by Zaxo (Archbishop) on Jul 14, 2002 at 03:19 UTC

    rename 'hamburger.txt', 'pizza.txt' or die $!; # of hunger

    After Compline,
    Zaxo

Re: Rename a file
by flounder99 (Friar) on Jul 14, 2002 at 15:53 UTC
    The builtin rename if fine for your example but if you want to rename ./burgerjoint/hamburger.txt -> ./pizzaplace/pizza.txt you will want to use move or mv in File::Copy

    use File::Copy qw( mv ); mv "./burgerjoint/hamburger.txt", "./pizzaplace/pizza.txt" or die $!;

    --

    flounder

Re: Rename a file
by DamnDirtyApe (Curate) on Jul 14, 2002 at 23:01 UTC
    $ perldoc -q rename Found in /usr/lib/perl5/5.6.1/pod/perlfaq5.pod How can I reliably rename a file? Well, usually you just use Perl's rename() function. That may not work everywhere, though, particularly when renam­ ing files across file systems. Some sub-Unix systems have broken ports that corrupt the semantics of rename()--for example, WinNT does this right, but Win95 and Win98 are broken. (The last two parts are not surprising, but the first is. :-) If your operating system supports a proper mv(1) program or its moral equivalent, this works: rename($old, $new) or system("mv", $old, $new); It may be more compelling to use the File::Copy module instead. You just copy to the new file to the new name (checking return values), then delete the old one. This isn't really the same semantically as a real rename(), though, which preserves metainformation like permissions, timestamps, inode info, etc. Newer versions of File::Copy exports a move() function.

    _______________
    D a m n D i r t y A p e
    Home Node | Email

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2014-08-29 02:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (275 votes), past polls