I am trying to write a program to find duplicate file names in multiple drive using perl. This is my script, but it gives wrong info.
#!/usr/bin/perl
use File::Find;
@filename;
@path;
$count;
open my $out, '>', 'output.txt' or die $!;
my $file = "E:";
find( \&edit, "$file" );
sub edit() {
$count++;
push( @filename, $_ );
push( @path, $File::Find::name );
}
print "Processing.";
for ( my $i = 0 ; $i < $count ; $i++ ) {
for ( my $j = $i + 1 ; $j < $count ; $j++ ) {
if ( $filename[$i] =~ /\Q$filename[$j]\E/ ) {
print $out("$filename[$i] = $filename[$j]\n");
print ".";
}
}
}
Kathir, the module File::Find::Rule is pretty powerful and easy to use. To find only mp3 files do this:
You should always include
use strict;
anduse warnings;
in EVERY perl script. However, you lucked and that didn't lead to any errors.In fact, other than using a regex to test when you should've used
eq
, your script looks functional. As a style change though, I would've saved all paths in a hash of arrays to more easily find matching files. Especially as currently your method wouldn't list groups of 3 or more together.