On 28 Oct 2005 at 10:05, Jay Savage wrote:

> 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?


Thanx jay,

I wasn't sure if it was me getting it wrong but what I am got with

 foreach my $name (grep {/^[^.](.*[a-z+].*)$/, $_ = $1} readdir (DIR) 
) 

 was:
...snip
/usr/local/spl/ -> /usr/local/spl/
/usr/local/spl/ -> /usr/local/spl/
/usr/local/spl/ -> /usr/local/spl/
/usr/local/spl/ -> /usr/local/spl/
Found 39 lowercase files

/usr/local/spl is a dir,  not a file, so I've really broken 
something.

Yes the T switch was giving me errors it the scripts former state. I 
can't run it now as I can't see what the files are...perhaps I should 
test it to see!

I have never used the taint switch before and it doesn't seem listed 
in the pragmas. Is there somewhere I can get some additional info 
about it?

Incidently mv doesn't work, its move. 

Should I throw out any additional arguments?


=========== Here what I have now ==========
#!/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";
if ($dir !~ /\/$/) {    # Add a slash if there is'nt one
                $dir = "$dir"."/";
}

# foreach my $name (grep {/^[^.](.*[a-z+].*)$/, $_ = $1} readdir (DIR) ) {
 foreach my $name (grep {/^[^.](.*[a-z]+.*)$/, $_ = $1} readdir(DIR)) {

                $name = uc($1);
                ++$found;
                (my $new = $name) =~ tr/[a-z]/[A-Z]/;  # trans the name
                $name = "$dir"."$name";
                $new = "$dir"."$new";
                if (-w $new) {
                        #move("$name","$new") or die "Can't upper $name: $!\n";
                }
                else {
                        print "$name isn't writable\n";
                }
                print "$name -> $new\n";
}
print "Found $found lowercase files\n";


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to