[comp.lang.perl] lowercase routine.

mdb@ESD.3Com.COM (Mark D. Baushke) (09/09/90)

On 7 Sep 90 15:31:31 GMT, pac@cathedral.cerc.wvu.wvnet.edu (Michael A.
Packer) said:

Michael> does anyone have a routine which can be compiled on UNIX that
Michael> will read the current directory and convert all upcase names
Michael> to lowercase?

Michael> if there was a file called 
Michael> READ.ME it would simply be converted to read.me

Michael> maybe some kind of script ???

A perl script perhaps? See below.
-- 
Mark D. Baushke
mdb@ESD.3Com.COM

#!/usr/local/bin/perl
#
# rename-uppercase-files.pl --- rename uppercase files to lowercase
# 
# Author          : Mark D. Baushke
# Created On      : Sat Sep  8 11:23:23 1990
# Last Modified By: Mark D. Baushke
# Last Modified On: Sat Sep  8 11:36:54 1990
# Update Count    : 1
# Status          : Alpha release 0.1
# 

# Convert any files in the given directories with uppercase file names
# into lowercase file names. Default to using the current directory.

unshift(@ARGV, '.') if $#ARGV < $[; # if no dir args use current directory
directory:
    while($ARGV = shift) {
    if (! -d ($dir = $ARGV)) {
	print STDERR "Usage: $0 [directory]...\n".
	    	     "$dir is not a directory...skipped\n";
	next directory;
    }

    # Get all files in the dir. Use readdir since it is faster than
    # globbing, but also because we should to include .-files.
    opendir (DIR, $dir) || die "$!: $dir\nStopped";
    @all = readdir (DIR);
    closedir (DIR);

    print STDERR "Processing '$dir' begins.\n";
    $cnt = 0;
    for $file ( @all ) {
	# If a file has an uppercase character in it it should be converted
	if ($file =~ /[A-Z]/) {
	    ($newfile = $file) =~ tr/A-Z/a-z/;
	    # Do not clobber any existing files
	    if ( -e $dir."/".$newfile ) {
		print STDERR "$dir/$newfile already exists. ".
		             "$dir/$file not renamed.\n";
	    }
	    else {
		rename($dir."/".$file, $dir."/".$newfile) ||
		    die "Unable to rename $dir/$file: $!";
		$cnt++;
	    }
	}
    }
    print STDERR "Processing '$dir' complete. $cnt files renamed.\n";
}