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

RE: [TCLUG:8884] converting uppercase to lowercase





> -----Original Message-----
> From: Carl Wilhelm Soderstrom [mailto:carls@agritech.com]
> Sent: Friday, October 01, 1999 3:16 PM
> To: tclug-list@mn-linux.org
> Subject: [TCLUG:8884] converting uppercase to lowercase
>
>
>         I have a directory tree full of files; the filenames are all in
> uppercase.
>         I wish all the filenames to be lowercase
>         I have a perl script (named mklc) that will convert uppercase to
> lowercase filenames:
>
> #!/usr/bin/perl
> foreach $filename(@ARGV)

>         $newfilename = lc($filename);
>         print "$filename -> $newfilename\n";
>         rename ($filename, $newfilename);
>         }
>
>         how do I make it recurse the entire tree, tho; and convert the
> filenames?
>

  I've tested this on a setup two directories deep...  I think it should
work anywhere.

#!/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);   # We need to have all our files at once,
                             # or recursion won't work correctly.

  foreach $file (@files) {
        chomp $file;
        $file =~ /^\./ and next;
        if ( -d $file ) {
                chdir($file);
                &recurse_dir('.');
                chdir('..');
        }
        elsif ( -f $file ) {
                $newfile = uc($file);
                if ($newfile ne $file) {
                 print "$file >> $newfile\n";
                 rename($file, $newfile) or warn "Couldn't rename $file:
$!\n";
                }
        }
 }

}