On 10/27/05, John W. Krahn <[EMAIL PROTECTED]> wrote: > Dermot Paikkos wrote: > > Hi, > > > > I wanted a script that would rename files from lower to upper case. I > > have something but I am a bit worried about going live with it as I > > can imagine there is plenty of room for error and I really don't want > > to knacker my filesystem. > > > > Could I get any comments of suggestion on what I have. I want > > something safe/secure. > > > > I am not sure about what would happen if it was passed more than one > > argument. Should I allow more than one argument? > > > > Are the any gotcha's to watch out for? > > Thanx, > > Dp. > > > > > > =============== upper.pl=============== > > > > #!/usr/bin/perl -Tw > > # upper.pl > > > > # Upper case all lowercase file in a given directory. > > > > > > use File::Copy; > > use strict; > > > > my $dir = shift; > > my $found = 0; > > > > opendir(DIR,$dir) or die "Can't open $dir: $!\n"; > > foreach my $name (sort grep !/^\./, readdir DIR) { # Credit Randal L. > > Schwartz > > if ($name =~ /[a-z]/) { # Look for lc file. Wot about > > files > > # with numbers?? > > > > ++$found; > > if ($dir !~ /\/$/) { # Add a slash if there is'nt > > one > > $dir = "$dir"."/"; > > } > > (my $new = $name) =~ tr/[a-z]/[A-Z]/; # trans the > > name > > $name = "$dir"."$name"; > > $new = "$dir"."$new"; > > #mv("$name","$new") or die "Can't upper $name: $!\n"; > > print "$name -> $new\n"; > > } > > > > } > > print "Found $found lowercase files\n" > > You probably want something like this: > > opendir DIR $dir or die "Can't open $dir: $!\n"; > > for my $name ( readdir DIR ) { > my $new = uc $name; > next if $new eq $name; > next if -e "$dir/$new"; > mv "$dir/$name", "$dir/$new" or die "Can't rename $dir/$name: $!\n"; > ++$found; > } >
First thnigs first: apparently my fingers didn't want to work yesterday. The loop invocation I really meant to write was: foreach my $name (grep {/^[^.](.*[a-z]+.*)$/, $_ = $1} readdir(DIR)) { But John's response has me wondering about that, since he usually picks up on the little things. The OP is setting the -T switch. readdir() returns tainted data, and rename()--and so I assume File::Copy::mv--won't accept tainted data and fails with "insecure dependencies" if passed files directly from readdir(). But none of the advice here has untainted $name. Is there some magic in uc() that I don't know about? Or is there something else I'm missing? -- jay -------------------------------------------------- This email and attachment(s): [ ] blogable; [ x ] ask first; [ ] private and confidential daggerquill [at] gmail [dot] com http://www.tuaw.com http://www.dpguru.com http://www.engatiki.org values of β will give rise to dom!