On 10/28/05, Dermot Paikkos <[EMAIL PROTECTED]> wrote:

>
> =========== 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;

You also need to untaint $dir if you're using taint checking see
perlre is you don't understand why we're taking a slice here:

    my $dir = (shift =~ /(.+)/)[0];

You should really do something more elaborate than that if you're
going to bother with taint checking at all, but that will let the
script run. See perlsec for all the gory details of taint checking.
The basic idea, though, is that with taint checking on Perl won't let
you use information that comes from outside the program (from the
command line or system calls) to execute external commands of modify
the filesystem, because such user input might be tainted. So before
you can use that data, you need to untaint it. And you normally do
that by having it pass a regex and capturing it in parentheses, with
the idea that you'll test to make sure the data is what you expect.
perlsec has the details.

>
> 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)) {

The only way to ignore directories is to test for them (see perlfun
'-X' for details) this would also be a good time :

    next if -d $name;

>                 $name = uc($1);

$name already has the value of $1. If you don't understand what grep
is doing, see perldoc -f grep. You should also see perlre for info
about using $1. Should be:

    $new = uc($name)

>                 ++$found;

>                 (my $new = $name) =~ tr/[a-z]/[A-Z]/;  # trans the name

tr/[a-z]/[A-Z]/; does the same thing as uc(). This is part of your
problem: your code destroys $name befure you use it in move.

>                 $name = "$dir"."$name";
>                 $new = "$dir"."$new";
>                 if (-w $new) {

This is backwards. A file can only be writable if it exists. That
means you will never create an uppercase file from a lowercase file.
You will only be able to clobber existing uppercase files. You almost
certainly wan't to make sure the file doesen't exist.

    if ( -e $new ) {
        print "Already did this one!\n";
            # because you may get dupes; see perlre for details of
what happens with $1
    } esle {

>                         #move("$name","$new") or die "Can't upper $name: 
> $!\n";
>                 }


>                 print "$name -> $new\n";
> }
> print "Found $found lowercase files\n";
>

Just to reiterate, That leaves us with:

    ==============================
    #!/usr/bin/perl -T

    use strict;
    use warnings;
    use File::Copy;

    my $dir = (shift =~ /(.+)/)[0];
    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)) {

        next if -d $name;
        $new = uc($name)

        ++$found;
        $name = "$dir"."$name";
        $new = "$dir"."$new";

        if ( -e $new ) {
            print "file already exists!\n";
        } esle {
            move("$name","$new") or die "Can't upper $name: $!\n";
        }
        print "$name -> $new\n";
    }
    print "Found $found lowercase files\n";
    ==============================

HTH,

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

Reply via email to