Tech2 Forums - Powered by vBulletin

Duplicate Files Deletor class

This is a topic on Duplicate Files Deletor class within the Programmers Corner forums, part of the Technology category; I recently had many photos duplicated and I was lazy to select each duplicate photo and then delete, so I ...

Results 1 to 4 of 4
Like Tree2Likes
  • 2 Post By imported_ksg91
  1. #1
    Uber Newbie
    Join Date
    Jul 2010
    Posts
    0
    Liked
    2 times
    Reputation points
    10

    Duplicate Files Deletor class

    I recently had many photos duplicated and I was lazy to select each duplicate photo and then delete, so I wrote a 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.
    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."
    "; 
            } 
        } 
      } 
      /*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(); 
    ?>
    hatZim and Kertison like this.

  2. #2
    Uber Newbie
    Join Date
    Jul 2010
    Location
    Kolkata
    Posts
    10
    Liked
    0 times
    Reputation points
    0

    Re: Duplicate Files Deletor class

    awesome!!!!!!!!but still m new to php

  3. #3
    Uber Newbie
    Join Date
    Jun 2012
    Posts
    1
    Liked
    0 times
    Reputation points
    10

    Re: Duplicate Files Deletor class

    Thanks buddy I was really looking for this script... it will help me a lot to clean up my mess!

    Quote Originally Posted by imported_ksg91 View Post
    I recently had many photos duplicated and I was lazy to select each duplicate photo and then delete, so I wrote a 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 finder pattern if you do not wish to delete important files.
    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."
    "; 
            } 
        } 
      } 
      /*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(); 
    ?>

  4. #4
    Tech2 Members ksg91's Avatar
    Join Date
    Mar 2012
    Location
    India
    Posts
    72
    Liked
    6 times
    Reputation points
    12

    Re: Duplicate Files Deletor class

    glad you liked it
    My Gear :Nokia N8

Similar Threads

  1. PROBLEM :: processor pins and pinless
    By raul_8 in forum PC Hardware
    Replies: 2
    Last Post: 09-29-2006, 01:04 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •