Skip to content

Instantly share code, notes, and snippets.

@michalbcz
Created November 11, 2022 09:14
Show Gist options
  • Select an option

  • Save michalbcz/68153cfd742aefc8cbdea80caf2b1ae4 to your computer and use it in GitHub Desktop.

Select an option

Save michalbcz/68153cfd742aefc8cbdea80caf2b1ae4 to your computer and use it in GitHub Desktop.

Revisions

  1. michalbcz created this gist Nov 11, 2022.
    47 changes: 47 additions & 0 deletions delete_duplicated.groovy
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,47 @@
    /*
    Delete duplicated files
    Naive implementation which consider duplicated files as files with same file size in bytes.
    Note: this implementation delete *.jpg files but you can easily change it with another regexp. See line 17.
    */

    import groovy.io.*

    def dir = new File(/your directory with duplicated files/)

    def filesOfSameSize = [:]

    println "Start processing files..."
    dir.eachFileRecurse(FileType.FILES, {
    if (it.name ==~ /.*\.jpg/) /* when you want delete just duplicated jpg files, change it on your will */ {
    def fileSizeInBytes = it.size()
    def fileSizeInMb = Math.round( fileSizeInBytes / (1024 * 1024 /* MBs */) * 1000) / 1000
    println "${it} (${fileSizeInMb} MB)"
    if (filesOfSameSize.containsKey(fileSizeInBytes)) {
    filesOfSameSize[fileSizeInBytes] << it
    } else {
    filesOfSameSize[fileSizeInBytes] = []
    filesOfSameSize[fileSizeInBytes] << it
    }

    }
    })

    println ""
    println "Looking for duplicates..."

    filesOfSameSize.each { size, files ->
    if (files.size() > 1) {
    files.sort({a, b -> a.name.size() <=> b.name.size()}) // the shortest name is always first

    // leave file with the shortest name and delete others
    files[1..-1].each { it ->
    println "Deleting duplicate ${it}"
    it.delete()

    // or if you are on Windows and has Java 9 you can use https://docs.oracle.com/javase/9/docs/api/java/awt/Desktop.html#moveToTrash-java.io.File-
    //java.awt.Desktop.getDesktop().moveToTrash(it)
    }
    }
    }