[Techtalk] Uppercase to lowercase

Jacinta Richardson jarich at perltraining.com.au
Thu Dec 5 13:58:19 EST 2002


> > Was wondering if anyone had a script they would like to share to convert 
> > upercase filenames to lowercase?
> 
> Here's one that might work for you:

I forgot some important error checking:

> ====== cut ======
> #!/usr/bin/perl -w
> use strict;
> use File::Copy;
> 
> 	# use "." for the path if you want the current directory.
> my $directory = "/some/path/to/files";
> opendir DIR, $directory or die "Failed to open $directory: $!";
> 
> 	# Take each file at a time (order is not guaranteed)
> foreach my $file (readdir DIR)
> {
> 		# don't bother with it if it's already lowercase
> 	next if ($file eq lc($file));
> 
> 		# get lowercase name and move it.
> 	my $lowercase_name = lc($file);

		# See if this will write over another file of the
		# lowercase name
        if(-e "$directory/$lowercase_name")
	{
		print STDERR "Moving $directory/$file to "
			     "$directory/$lowercase_name will overwrite "
			     "existing file $directory/$lowercase_name";
	 	next;
	}

		# Else it's safe to move.
> 	move("$directory/$file", "$directory/$lowercase_name");
> }
> exit 0;
> ====== cut ======

--
   ("`-''-/").___..--''"`-._          |  Jacinta Richardson	    |
    `6_ 6  )   `-.  (     ).`-.__.`)  |  Perl Training Australia    |
    (_Y_.)'  ._   )  `._ `. ``-..-'   |      +613 9354 6001 	    |  
  _..`--'_..-_/  /--'_.' ,'           | contact at perltraining.com.au |
(il),-''  (li),'  ((!.-'              |   www.perltraining.com.au   |




More information about the Techtalk mailing list