Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Re: rename files in directory with new extenstion

by mugwumpjism (Hermit)
on Aug 09, 2001 at 18:14 UTC ( [id://103407]=note: print w/replies, xml ) Need Help??


in reply to rename files in directory with new extenstion

I always figured, if you're going to do it, do it right:

#!/usr/bin/perl use strict; my $pattern = shift @ARGV; my ($src, $dest) = ($pattern =~ m/^s(.)([^\1]*)\1([^\1]*)\1$/) or die "1st arg not s/// construct (or construct too complicated)" +; my %to_rename; for my $f (@ARGV) { (! -e $f) && die "$f does not exist; $!"; my $n = $f; $n =~ s/$src/$dest/; ( -e $n or defined $to_rename{$n}) && die "$f would be renamed to $n; clobber detected"; $to_rename{$n} = $f; } while (my ($n, $f) = each %to_rename) { rename $f, $n or warn "Cannot rename $f to $n; $!"; }

OK, I can see bad style there, but it works. I wrote it some time ago...

I called the script ~mv, and it is called as ~mv s/\.old$/.new/ *.old

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (8)
As of 2024-04-18 06:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found