[Gllug] Perl scripting challenge

Greg McCarroll greg at mccarroll.org.uk
Wed Oct 4 15:33:12 UTC 2006


THIS IS VERY MUCH UNTESTED!!

Basically use F::F::R to find all files in a dir ($DIR), iterate over
them, use a nasty regex to find their filename (there is a better way
to do this, i just can't be bothered remembering) and then the bit
that actually achieves what you want. Take the filename lowercase it
all or uppercase it all and create a hash using this value as the key.
The hash is a hash of arrays, so you push the full pathname including
the originally cased filename into it.

Hope this is what you were looking for,

Greg

#!/opt/local/bin/perl
use File::Find::Rule;
use Data::Dumper;

use strict;
use warnings;

my $DIR = '.';

my @files = File::Find::Rule->file()->in( $DIR );

my $hash = {};

foreach my $filename_and_dir (@files) {

   my $filename;
   # THIS NEXT LINE IS NOT PORTABLE AND THERE WILL BE SOMETHING
   # ON CPAN TO DO IT RIGHT.
   ($filename = $filename_and_dir) =~ s/.*\///;

   $filename = lc($filename); # make it case insensitive;

   push(@{$hash->{$filename}},$filename_and_dir);
}

print Dumper($hash);




On 4 Oct 2006, at 16:08, Simon Morris wrote:

> Hello,
>
> A quick plea/challenge to those of you who are Perl minded.
>
> I need a script that searches a file system looking for files that are
> named like so:
>
> foo.txt
> Foo.txt
> FOO.txt
> foO.tXt
>
> and so on in the same directory. Apparently someone thought that this
> would be a good idea to do!
>
> The script just needs to report the duplicated instances rather than
> actually do anything to the file and I'd like to be able to call it  
> like
> this:
>
> ./magic_script.pl /home
>
> to run the script against the contents of the /home directory.
>
> Thanks
>
> ~sm
>
> -- 
> Gllug mailing list  -  Gllug at gllug.org.uk
> http://lists.gllug.org.uk/mailman/listinfo/gllug

-- 
Gllug mailing list  -  Gllug at gllug.org.uk
http://lists.gllug.org.uk/mailman/listinfo/gllug




More information about the GLLUG mailing list