I’ve used these in the past when merging file shares from multiple sites to a single site.

The scenario:
Three sites, each with the same folder hierarchy (A-Z).

Each office generally dealt with separate clients but there were still duplicates to be found. To save manually auditing 100s of folders, I used a combination of a simple dir command and the PHP scripts below.

To output all folders I used: dir /s /AD /b > textfile.txt on each file server. This gave me 3 text files to compare. Using the PHP script to compare these files displayed all of the duplicates, from here I could manually sort out any duplicates before merging the 3 file shares.

To find duplicates from within one text file:
<?php
$file = file(‘textfile.txt’);
$a = array();
foreach($file as $f)
{
++$a[strtolower(trim($f))];
}
foreach($a as $key => $value)
{
if($value > 1)
{
echo $key . PHP_EOL;
}
}

To compare two text files:
<?php
$a = file(‘textfile1.txt’);
$b = file(‘textfile2.txt’);
foreach($a as $key => $value)
{
$a[$key] = strtolower($value);
}
foreach($b as $key => $value)
{
$b[$key] = strtolower($value);
}
foreach(array_intersect($a, $b) as $x) { echo $x; }

To compare three text files:
<?php
$a = file(‘textfile1.txt’);
$b = file(‘textfile2.txt’);
$c = file(‘textfile3.txt’);
foreach($a as $key => $value)
{
$a[$key] = strtolower($value);
}
foreach($b as $key => $value)
{
$b[$key] = strtolower($value);
}
foreach($c as $key => $value)
{
$c[$key] = strtolower($value);
}
foreach(array_intersect($a, $b, $c) as $x) { echo $x; }