Created
November 11, 2022 09:14
-
-
Save michalbcz/68153cfd742aefc8cbdea80caf2b1ae4 to your computer and use it in GitHub Desktop.
Revisions
-
michalbcz created this gist
Nov 11, 2022 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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) } } }