Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: File Extension Renaming

by ciderpunx (Vicar)
on Jul 05, 2006 at 15:55 UTC ( [id://559377]=note: print w/replies, xml ) Need Help??


in reply to File Extension Renaming

You're changing the value of $zips, but not the name of the file. You're maybe after something like:
#!/usr/bin/perl $dir = "./test"; opendir(DIR,$dir) || die "NO SUCH Directory: $dir\n$!"; foreach $zips (readdir DIR){ $new_name = $zips; $new_name =~ s/\.log/\.txt/ig; rename "$dir/$zips","$dir/$new_name" || die("Can't rename $zips\n$ +!"); } close(DIR);
HTH

Replies are listed 'Best First'.
Re^2: File Extension Renaming
by japhy (Canon) on Jul 05, 2006 at 15:59 UTC
    In your rename ... || die, the || binds too tightly: it binds to the "$dir/$new_name" string, not to the return value of rename(). Either use rename(...) || die or rename ... or die.

    Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
    How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart
Re^2: File Extension Renaming
by davidrw (Prior) on Jul 05, 2006 at 17:14 UTC
    Should make that
    $new_name =~ s/\.log/\.txt/ig or next;
    so that it skips non-.log files .. otherwise the s/// will fail and rename will have identical OLDNAME and NEWNAME arguments.
Re^2: File Extension Renaming
by jdtoronto (Prior) on Jul 05, 2006 at 17:48 UTC
    Changing your code to this will avoid the name collision problem - rename will overwrite a duplicate named file.
    #!/usr/bin/perl $dir = "./test"; opendir(DIR,$dir) || die "NO SUCH Directory: $dir\n$!"; foreach $zips (readdir DIR){ $new_name = $zips; $new_name =~ s/\.log/\.txt/ig; rename ("$dir/$zips","$dir/$new_name") unless ($zips eq $new_name) +; } close(DIR);
    jdtoronto

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (5)
As of 2024-03-28 16:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found