Not sure if this helps any of you - or if you have some feedback.I've
written a few versions of this over the years but this is ChatGPT doing it.

Useful if you have same named files which are different and different named
files that are the same file.

Thanks

P



<?php
if ($argc !== 3) {
    die("Usage: php script.php /path/to/files /path/to/backup\n");
}

$sourceDir = $argv[1];
$backupDir = $argv[2];

if (!is_dir($sourceDir) || !is_dir($backupDir)) {
    die("Both arguments must be directories.\n");
}

function getFilesRecursive($dir) {
    $files = [];
    $iterator = new RecursiveIteratorIterator(new
RecursiveDirectoryIterator($dir));
    foreach ($iterator as $file) {
        if ($file->isFile()) {
            $files[] = $file->getPathname();
        }
    }
    return $files;
}

function moveFile($file, $backupDir) {
    $fileName = basename($file);
    $randomString = bin2hex(random_bytes(8));
    $newPath = $backupDir . DIRECTORY_SEPARATOR . $randomString . '-' .
$fileName;
    if (!rename($file, $newPath)) {
        echo "Failed to move $file to $newPath\n";
    } else {
        echo "Moved $file to $newPath\n";
    }
}

$files = getFilesRecursive($sourceDir);
$fileInfo = [];

foreach ($files as $file) {
    $md5 = md5_file($file);
    $size = filesize($file);
    $type = mime_content_type($file);

    $key = $md5 . '_' . $size . '_' . $type;

    if (isset($fileInfo[$key])) {
        moveFile($file, $backupDir);
    } else {
        $fileInfo[$key] = $file;
    }
}

echo "Duplicate check and move completed.\n";
?>
_______________________________________________
luv-main mailing list -- luv-main@luv.asn.au
To unsubscribe send an email to luv-main-le...@luv.asn.au

Reply via email to