Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Can't rename all directories but only some

by kirk123 (Beadle)
on Aug 23, 2002 at 00:28 UTC ( #192226=perlquestion: print w/replies, xml ) Need Help??

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


I am trying to rename the following directories:

C:\elmPaul\jod25573786-2k2 C:\elmPaul\jod25573786-2k2\wed25573786-2k2

I'm unable to rename both of these directories using :

finddepth (\&update_file , "C:\\elmPaul" ); sub update_file { if( /(.*)$old_hostname(.*)/i ) { $concat = "$1$new_hostname$2"; rename($_, $concat) || print "error can't renames:$!"; }

But I am able to rename ONLY "C:\elmPaul\jod25573786-2k2\wed25573786-2k2" using:

sub update_file { if( /(.*?)$old_hostname(.*?)/i ) { $concat = "$1$new_hostname$2"; rename($_, $concat) || print "error can't renames:$!"; }

Does anybody knows why? Please advise.


Edited 2002-08-22 by Ovid

Replies are listed 'Best First'.
Re: Can't rename all directories but only some
by chromatic (Archbishop) on Aug 23, 2002 at 01:11 UTC

    $! knows. What does it say?

      The error I get is permission denied. --kirk
        Then you do not have permission on your system to change the name of the directory. You should check your file permissions/ownership then try again. Can you manually mv the file? etc. etc.

        perl -e "print qq/just another perl hacker who doesn't grok japh\n/"

Re: Can't rename all directories but only some
by sauoq (Abbot) on Aug 23, 2002 at 02:00 UTC

    Reading the code was kind of difficult given the lack of code tags so I'll just take some wild guesses. First, you won't be able to change the name of the directory if it is being used. For instance, if you have a dos window opened and you are CD'd to that directory. You may even need to use find() with the no_chdir option.

    Other than that, does your user have permissions to change the directory name? Have you tried it as Administrator? I have to confess that I know pretty much nothing about permissions on Windows.

    "My two cents aren't worth a dime.";
Re: Can't rename all directories but only some
by helgi (Hermit) on Aug 23, 2002 at 13:05 UTC
    I second that you should check permissions (especially important if this is a stealth CGI question, because the web server runs as an anonymous user with greatly restricted rights).

    Another hugely important point is the way you handle filenames:

    Do not, I repeat NOT use backslashes (\) as the directory separators with Perl under Windows. Use normal forward slashes (/) unless you really want to get anal about replacing each and every occurence of backslash (\) with double backslash (\\) each and every time.

    When you perform a system function on a file, such as rename, you should always return, not only the error ($!), but also the filename itself, like so:

    rename $old,$new or die "Cannot rename $old to $new:$!\n";

    This is because the most frequent cause of error in such cases is a malformed filename.

    For example if you tried to rename your file: C:\elmPaul\jod25573786-2k2 and returned the name, you would find that you were actually trying to rename the file:
    backslash e = ESC
    backslash j = no special meaning

    which probably does not exist.


    Helgi Briem

Re: Can't rename all directories but only some
by greenFox (Vicar) on Aug 23, 2002 at 03:52 UTC
    The only difference I can see between your two approaches is the non greedy matches on your re in the second case. If you aren't getting a permission denied error with the first code then perhaps $concat isn't being set to what you think? My advice is for your error messages to always say exactly what you were trying to do ie.

    rename($_, $concat) || print "Error could not rename $_ to $concat : $ +!";

    When you give us an error message please copy and paste it, don't paraphrase it. The error message could contain the clue that somebody will see and that you are missing.

    Until you've lost your reputation, you never realize what a burden it was or what freedom really is. -Margaret Mitchell

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://192226]
Approved by Rex(Wrecks)
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (5)
As of 2023-06-01 08:05 GMT
Find Nodes?
    Voting Booth?

    No recent polls found