TCLUG Archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: [TCLUG:8884] converting uppercase to lowercase



The code I sent earlier won't rename directories.  Here's a version that
will.


#!/usr/bin/perl -w

if (@ARGV == 0) {recurse_dir('.')}
else {
 while(<>) {recurse_dir($_)}
}

sub recurse_dir {
 my $dir = shift;
 opendir(DIR, $dir) or warn "Couldn't open $dir: $!\n";
 my @files = readdir(DIR);

  foreach $file (@files) {
        chomp $file;
        $file =~ /^\./ and next;   # ignore "." ".." and hidden files
        if ( -d $file ) {
                chdir($file);
                recurse_dir('.');
                chdir('..');
                do_rename($file);
                }
        }
        elsif ( -f $file ) {do_rename($file)} }  # probably safe to just use
								 # "else {do_rename($file)} but
								 # I didn't feel like finding out.
 }

}

sub do_rename {
 my $foo = shift;
 my $bar = lc($foo);
 if ($foo ne $bar) {
        print "$foo >> $bar\n";
        rename ($foo, $bar) or warn "Couldn't rename $foo: $!\n";
 }
}