Skip to content

Instantly share code, notes, and snippets.

@erikfrerejean
Created January 29, 2013 16:35
Show Gist options
  • Save erikfrerejean/4665607 to your computer and use it in GitHub Desktop.
Save erikfrerejean/4665607 to your computer and use it in GitHub Desktop.
<?php
namespace erikfrerejean\musicunique;
// Setup quick 'n dirty autoloader
spl_autoload_register(function($class) {
$namespace = 'erikfrerejean\\musicunique\\';
if (strpos($class, $namespace) !== 0)
{
return;
}
$class = substr($class, strlen($namespace));
require __DIR__ . "/$class.php";
}, true, true);
// Absolute path to the music directory
$path = '/Volumes/Macintosh/iTunes/Music/';
// Simple filter
class MusicuniqueFilter extends \RecursiveFilterIterator
{
private static $found = array();
public function __construct(\RecursiveDirectoryIterator $iter)
{
parent::__construct($iter);
}
public function accept()
{
// Directories don't get tested
if ($this->current()->isDir())
{
return true;
}
// Filename
$filename = $this->current()->getFileName();
// Don't bother with hidden files
if ($filename[0] === '.')
{
return false;
}
// Remove the extension
$basename = $this->current()->getBasename(
'.' . $this->current()->getExtension()
);
// We'll be removing all files *not* ending on ` 2`
if (substr($basename, -2) !== ' 2')
{
return false;
}
return true;
}
}
// Loop through all the files
$iterator = new \RecursiveIteratorIterator(
new MusicuniqueFilter(
new \RecursiveDirectoryIterator($path)
)
);
$found = array();
foreach ($iterator as $fileinfo)
{
array_push($found, $fileinfo->getPathname());
}
// dump
foreach ($found as $f)
{
if (file_exists($f))
{
echo "Delete [$f]\n";
unlink($f);
}
}
exit;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment