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}; }
Output 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
There is only one P12345679 directory

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


In reply to Using file::find to find dupe dirs by RockE

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.