Beefy Boxes and Bandwidth Generously Provided by pair Networks DiBona
Think about Loose Coupling
 
PerlMonks  

rename files in directory with new extenstion

by c (Hermit)
on Aug 09, 2001 at 08:20 UTC ( #103328=perlquestion: print w/ replies, xml ) Need Help??
c has asked for the wisdom of the Perl Monks concerning the following question:

@array = glob(*.old); while(<@array>) { $newext = $_; $newext =~ s/old/new/; rename($_, $newext); }

I think I have this pretty short, however, I've learned that often there are shorter and easier ways to do something. If anyone can point out a shorter path to the goal I seek, it would be appreciated.

humbly -c

Comment on rename files in directory with new extenstion
Download Code
Re: rename files in directory with new extenstion
by damian1301 (Curate) on Aug 09, 2001 at 08:37 UTC
    you could just do
    while(<*.old>){ ($new = $_) =~ s/old$/new/; rename($_,$new); }
    And not waste space by bringing all those filenames into an array

    $_.=($=+(6<<1));print(chr(my$a=$_));$^H=$_+$_;$_=$^H; print chr($_-39); # Easy but its ok.
Re (tilly) 1: rename files in directory with new extenstion
by tilly (Archbishop) on Aug 09, 2001 at 08:42 UTC
    And what if you have a file named, "old.old"?

    On older Perl's, what if your directory exceeds the limit of the shell?

    And before using glob freely, I like worrying about what happens if someone use a space in a path. I see no reason to open myself up to that.

    As you can tell, I like to be the kind of person who anticipates potential problems and heads them off at the pass. So even though it isn't so short, I tend to use an explicit opendir/readdir/grep solution rather than globbing. YMMV.

Re: rename files in directory with new extenstion
by mugwumpjism (Hermit) on Aug 09, 2001 at 18:14 UTC

    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

Re: rename files in directory with new extenstion
by Hofmator (Curate) on Aug 09, 2001 at 18:29 UTC

    For a very flexible approach to renaming see the cookbook, recipe 9.9:

    #!/usr/bin/perl -w # rename - Larry's filename fixer $op = shift or die "Usage: rename expr [files]\n"; chomp(@ARGV = <STDIN>) unless @ARGV; for (@ARGV) { $was = $_; eval $op; die $@ if $@; rename($was,$_) unless $was eq $_; }
    which allows for such nice calling examples as (also from the Cookbook):
    % rename 's/\.orig$//' *.orig % rename 'tr/A-Z/a-z/ unless /^Make/' * % rename '$_ .= ".bad"' *.f % rename 'print "$_: "; s/foo/bar/ if <STDIN> =~ /^y/i' * % find /tmp -name '*~' -print | rename 's/^(.+)~$/.#$1/'

    -- Hofmator

Re: rename files in directory with new extenstion
by George_Sherston (Vicar) on Aug 09, 2001 at 23:09 UTC
    This does it in one line. Also I wonder if, in BIG applications, you might get a time saving by avoiding regexp? This is not a rhetorical question - I'd be interested to know.

    while($f=<*.old>){rename $f, substr($f,0,-3).'new'}


    George Sherston

      This would not work for file with extensions longer than 3 characters (eg any .html file). Not that I really need to point that out...

      note: happy 100th node, me

Re: rename files in directory with new extenstion
by George_Sherston (Vicar) on Aug 09, 2001 at 23:19 UTC
    Agreed - but one has to initialise 'old' and 'new', so at the same time can't one just set '-3' to '-4' (or whatever)?

    George Sherston

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (8)
As of 2014-04-18 08:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (464 votes), past polls