[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: [Omaha.pm] Rename [x] files in a directory



Eh, you have to modify yours for every situation.  Here's a more generic approach:

> less -x4 ~/rename.pl
#!/bin/perl

    if( -t && scalar(@ARGV)==0 ) {
        print " Usage: rename 'glob' operation\n";
        print "   where 'glob' is a glob file selector\n";
        print "   and operation is typically a substitution command\n";
        print "\n   example:\n    rename '*.cfg' 's/\\.cfg/\\.xyz/'\n";
    }

    my @files=glob($ARGV[0]);

    while ($fname=shift @files) {
        $new=$fname;
        
        eval "\$new =~ $ARGV[1]";
        print "$fname -> $new\n";
        rename $fname, $new;
    }


-Scott

-----Original Message-----
From: omaha-pm-bounces@mail.pm.org
[mailto:omaha-pm-bounces@mail.pm.org]On Behalf Of Jay Hannah
Sent: Thursday, September 09, 2004 4:13 PM
To: omaha-pm@pm.org
Subject: [Omaha.pm] Rename all files in a directory



Another 5 minute hack...

j

#!/usr/bin/perl

opendir DIR, ".";
@files = grep /^ppm/, readdir DIR;
closedir DIR;

foreach (@files) {
   $new = $_;
   $new =~ s/^ppm/its/;
   print "rename $_ $new\n";
   rename $_, $new;
}





_______________________________________________
Omaha-pm mailing list
Omaha-pm@mail.pm.org
http://www.pm.org/mailman/listinfo/omaha-pm