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

rename doesn't work on OSX?

by mmfuller (Initiate)
on Dec 10, 2009 at 01:57 UTC ( #812079=perlquestion: print w/ replies, xml ) Need Help??
mmfuller has asked for the wisdom of the Perl Monks concerning the following question:

I don't use perl much. I tried to use it to rename some files and I can't get the rename function to work. The problem seems to be file permissions, but even if I use sudo, or chmod the permissions to 755, it still won't work. Does this have something to do with OSX and Darwin? I am running 10.6.2 on an Intel Macbook.

My code (the directory "kennel" contains two files: cat1, cat2):

opendir(DIR,"kennel"); while ($file = readdir(DIR)) { next unless $file =~ /cat/; my $newfile = $file; $newfile =~ s/cat/dog/; rename($file, $newfile) || print "Don't have permission to rename. +\n"; } closedir(DIR);
When run, this code prints the error message (i.e. does not rename files)

Comment on rename doesn't work on OSX?
Download Code
Re: rename doesn't work on OSX?
by toolic (Chancellor) on Dec 10, 2009 at 02:09 UTC
    You can try to get a more specific error message by also printing the $! variable:
    rename($file, $newfile) || print "Don't have permission to rename: $!\ +n";

    According to the documentation for rename,

    Behavior of this function varies wildly depending on your system implementation. For example, it will usually not work across file system boundaries, even though the system mv command sometimes compensates for this. Other restrictions include whether it works on directories, open files, or pre-existing files. Check perlport and either the rename(2) manpage or equivalent system documentation for details. For a platform independent move function look at the File::Copy module.
    You could try File::Copy.
Re: rename doesn't work on OSX?
by GrandFather (Cardinal) on Dec 10, 2009 at 02:10 UTC

    permissions is a good guess, but

    rename($file, $newfile) || print "Rename $file to $newfile failed: + $!.\n";

    will give you a lot more information including what the system thinks the problem is.


    True laziness is hard work
Re: rename doesn't work on OSX?
by desemondo (Hermit) on Dec 10, 2009 at 03:01 UTC
    If all else fails you should be able to use your OS's rename function via an exec , system , or `rename $file1 $file2` (backtick) workaround... I'd use that only as a last resort though...
Re: rename doesn't work on OSX?
by ikegami (Pope) on Dec 10, 2009 at 03:07 UTC
    The actual error (as indicated by $!) is that the file you asked to rename doesn't exist. You're trying to rename kennel/cat, but you're telling Perl and the system to rename cat.
Re: rename doesn't work on OSX?
by vitoco (Friar) on Dec 10, 2009 at 11:59 UTC

    File path is missing from parameters. Try:

    my $dir = "kennel"; opendir(DIR,$dir) or die "Cannot read $dir\n"; while ($file = readdir(DIR)) { next unless $file =~ /cat/; my $newfile = $file; $newfile =~ s/cat/dog/; rename("$dir/$file", "$dir/$newfile") || print "Don't have permiss +ion to rename.\n"; } closedir(DIR);

      @vitoco and @ikegami -- the file path! Right! It's usually the simplest little things that trip me up!

      Thank you!!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (9)
As of 2014-12-28 04:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (178 votes), past polls