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

Miller, Scott L (Omaha Networks) scott.l.miller at hp.com
Thu Sep 9 16:35:25 CDT 2004


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 at mail.pm.org
[mailto:omaha-pm-bounces at mail.pm.org]On Behalf Of Jay Hannah
Sent: Thursday, September 09, 2004 4:13 PM
To: omaha-pm at 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 at mail.pm.org
http://www.pm.org/mailman/listinfo/omaha-pm



More information about the Omaha-pm mailing list