Duplicate Files Deletor

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Duplicate Files Deletor

    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 Code:
    <?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.
    Attached Files
    Last edited by ksg91; 27.02.11, 10:14. Reason: little improvement in code
    Follow me @ksg91 | My Blog: http://ksg91.com | Nokia Blog: http://NokiaTips.in
Working...
X