Re: Is this script safe?
- From: krahnj@xxxxxxxxx (John W. Krahn)
- Date: Thu, 27 Oct 2005 13:32:08 -0700
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;
}
John
--
use Perl;
program
fulfillment
.
- References:
- Is this script safe?
- From: Dermot Paikkos
- Is this script safe?
- Prev by Date: Re: what happened to pdl.perl.org?
- Next by Date: Re: Binary Large Objects (BLOB) decoding
- Previous by thread: Re: Is this script safe?
- Next by thread: Re: Is this script safe?
- Index(es):
Relevant Pages
|