Duplicate Files Deletor

Started on the old forum in February 2011 · view the original in the archive · old links redirect here

No replies · 573 views · started 15 years ago · 1 min read

ksg91 Developer · 162 posts #1

I recently had many photos duplicated and I was lazy to select each duplicate photo and then delete so I wrote small php class to do the job. 🙂 You need to instantiate the class with directory that contain duplicate files and the pattern that is unique to duplicate file.

WARNING: Be careful with duplicate file's pattern if you do not wish to delete important files.

<?php
/**
 *$Author: Ksg91 $
 *$Date: 2011-02-27 14:00 +0530 $
 * 
 *This simple class deletes files that has specific sub string in its file name.
 *I used it to delete duplicate photos. 
 *You may edit it as per your need. 
 *
 *WARNING: Be careful while choosing pattern for duplicate files. 
 *         If the pattern you choose is not unique to duplicates, you will
 *         lose unique and important files.    
 */
class DuplicateDelete
{
  private $dir,$handle,$dupPattern,$currentFile;
  /*constructor to set directory in which files are to be searched and subsring 
   that a duplicate file will contain. */
  function __construct($dir,$dupPattern)
  {
    $this->dir=$dir;
    $this->handle=opendir($dir);
    $this->dupPattern=$dupPattern;
  }
  /*This function will delete all duplicate files*/
  function deleteDuplicates()
  {
    while (false !== ($this->currentFile = readdir($this->handle))) {
        if($this->isDuplicate()){
          unlink($this->dir."\\".$this->currentFile);
          //echo $this->currentFile."<br />";
        }
    }
  }
  /*This function will check if currentFile is duplicate. Returns true if 
    duplicate, else false. */ 
  function isDuplicate()
  {
    if(strstr($this->currentFile,$this->dupPattern))
      return true;
    else 
      return false;
  }
}

/* EXAMPLE */

/* I had photos and videos that were somehow duplicated and all duplicate photos/videos
  contained _2 at the end of file. So the substring for those duplicate photos/videos
  would be "_2." */
$dd=new DuplicateDelete("E:\\Robo-Opus 11\\New folder\\New folder","_2."); 
$dd->deleteDuplicates();
?>

Small improvement has been done. It isn't updated in attachment.

Follow me @ksg91 | My Blog: http://ksg91.com | Nokia Blog: http://NokiaTips.in

Attachments: duplicate.zip

Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close