Automatic deletion of duplicates of files
Posted: 02 Jun 2015 19:28
How can I delete duplicates automatically? Say, having found a list of duplicate files, I want to delete of each pair the one that has the shorter path?
Forum for XYplorer Users and Developers
https://www.xyplorer.com/xyfc/
Code: Select all
$duplicates = <<<>>>
D:\Temp\I am a duplicate.zip
D:\Temp\subfolder\I am a duplicate.zip
D:\C++\Hello World.cpp
D:\Hello World.cpp
>>>;
$delList = "";
foreach($file, $duplicates, "<crlf>") {
if !($file) { continue; }
$duplicates = replace($duplicates, $file);
$match = regexmatches($duplicates, "^.*?" . regexEscape(getpathcomponent($file, "file")) . "$");
if ($match) {
$delFile = (strlen($file) > strlen($match)) ? $match : $file;
$delList = $delList . $delFile . "|";
}
}
if ($delList) { $confirm = confirm("Do you really want to delete these files:||$delList", "|", 2); }
if ($confirm) { delete 1, 0, $delList; }
function regexEscape($string) {
return regexreplace($string, "([\\^$.+*|?(){\[])", "\$1"); }
}Code: Select all
$duplicates = <<<>>>
D:\Temp\I am a duplicate.zip
D:\Temp\subfolder1\I am a duplicate.zip
D:\Temp\subfolder2\I am a duplicate.zip
D:\C++\Hello World.cpp
D:\Hello World.cpp
>>>;
$delList = $duplicates; // Make a copy of the duplicate list
foreach($file, $duplicates, "<crlf>") {
if !($file) { continue; }
$pattern = "^.*?" . regexEscape(getpathcomponent($file, "file")) . "$";
$matches = regexmatches($duplicates, $pattern);
if ($matches) {
$lastLen = "";
foreach($match, $matches) { // Compare length of matches and only keep the one with the longest path
$curLen = strlen($match);
if ($curLen > $lastLen) { $keepFile = $match; }
$lastLen = $curLen;
}
$delList = regexreplace($delList, "^" . regexEscape($keepFile) . "$"); // Delete the file to keep from the delete list
$duplicates = regexreplace($duplicates, $pattern); // Delete all matches from the original list
}
}
$delList = replace(formatlist($delList, "e", "<crlf>"), "<crlf>", "|");
if ($delList) { $confirm = confirm("Do you really want to delete these files:||$delList", "|", 2); }
if ($confirm) { delete 1, 0, $delList; }
function regexEscape($string) {
return regexreplace($string, "([\\^$.+*|?(){\[])", "\$1"); }
}