RockE has asked for the wisdom of the Perl Monks concerning the following question:
I am trying to get this to work but it's not only printing out all instances of a duplicate directory but its also printing out a directory that doesn't have any duplicates... Any help to get this thing to work would be appreciated. I'm unable to install any of the other related file:find modules at work so file:find is it.
#!/usr/bin/perl # dirdupes use strict; use warnings; use File::Find; #*****************Path Variables********************** our $testpath = 'C:\\Temp\\'; #******************************************************* my %dirs; find(\&dir_names, $testpath); my @dup_dirs = grep { $dirs{$_} > 1 } sort keys %dirs; foreach my $list (@dup_dirs) { print "$list\n"; } sub dir_names { return unless -d; return unless /[IPD]\d{8}$/; $dirs{$File::Find::name} = 1; ++$dirs{$File::Find::name}; }
There is only one P12345679 directoryOutput c:\Temp\hddzip>perl dirdupes.pl C:\Temp/Alcohol/P12345678 C:\Temp/hddzip/I12345678 C:\Temp/hddzip/P12345678 C:\Temp/hddzip/P12345679 C:\Temp/hddzip/Test1/P12345678 C:\Temp/hddzip/Test2/P12345678
**Update** I fiddled around some more and got this to work.
#!/usr/bin/perl # dirdupes use strict; use warnings; use File::Find; #*****************Path Variables********************** our $testpath = 'C:/Temp/'; #******************************************************* my %dirs; find(\&dir_names, $testpath); foreach my $D (sort keys %dirs) { if (@{$dirs{$D}} > 1) { print "\nFound duplicate directories\n"; foreach my $dupes (@{$dirs{$D}}) { print "$D - $dupes\n"; } } } sub dir_names { return unless -d ; return unless /([IPD]\d{8})$/; my $P00 = $_; #or $1 if (not exists($dirs{$P00})) {$dirs{$P00}=[];} push @{$dirs{$P00}},$File::Find::dir; }
C:\Temp\hddzip>perl dirdupes.pl Found duplicate directories I12345678 - C:/Temp/hddzip I12345678 - C:/Temp/hddzip/Test1 Found duplicate directories P12345678 - C:/Temp/Alcohol P12345678 - C:/Temp/hddzip P12345678 - C:/Temp/hddzip/Test1 P12345678 - C:/Temp/hddzip/Test2 Found duplicate directories P12345679 - C:/Temp/hddzip P12345679 - C:/Temp/hddzip/PDF
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Using file::find to find dupe dirs
by kcott (Archbishop) on Nov 14, 2013 at 07:36 UTC | |
| |
|
Re: Using file::find to find dupe dirs
by GrandFather (Saint) on Nov 14, 2013 at 01:46 UTC | |
by RockE (Novice) on Nov 14, 2013 at 05:41 UTC | |
|
Re: Using file::find to find dupe dirs
by boftx (Deacon) on Nov 14, 2013 at 01:50 UTC |