[Home]RegExp/FileRename

ec2-44-210-99-209.compute-1.amazonaws.com | ToothyWiki | RegExp | RecentChanges | Login | Webcomic

(Why not use the rename.pl script that comes with Perl?)
'cos that involves being aware of its existence? Besides, it doesn't seem to be there in the default cygwin binary package.. ^^; - MoonShadow

slewis@SLEWIS ~
$ locate rename.pl

slewis@SLEWIS ~
$ perl -v

This is perl, v5.6.1 built for cygwin-multi




#! perl -w
use English;
use strict;

# stick this in file.pl
# then either chmod +x file.pl; mv file.pl /usr/local/bin   so file.pl runs it
# or perl file.pl 

my $dir = "./"; # <----- directory to do the rename in, must end in /

opendir DIR, $dir or die "Couldn't open directory $dir!\n";
my @entries = readdir DIR; # read directory entries into array
closedir DIR;

foreach my $direntry (@entries)
{
  if (-f $dir.$direntry) # if it's a file
  {
    my $newname = $direntry; # make a copy of the name
    if ($newname =~ s/ /_/g) # if the regexp managed to substitute anything..
    #               ^^^^^^^ (your rename regexp goes here)
    {
      rename $dir.$direntry, $dir.$newname # ...rename the file
        or print "Warning: couldn't rename $dir.$direntry to $dir.$newname.\n"; 
    }
  }
}

# ------ 8< --------- cut here --------- 8< ------



ec2-44-210-99-209.compute-1.amazonaws.com | ToothyWiki | RegExp | RecentChanges | Login | Webcomic
Edit this page | View other revisions | Recently used referrers
Last edited October 2, 2003 9:42 am (viewing revision 5, which is the newest) (diff)
Search: