Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

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?)

Replies are listed 'Best First'.
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,

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 $!;



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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://181548]
Approved by virtualsue
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (6)
As of 2018-01-20 17:57 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (227 votes). Check out past polls.