Skip to content

Instantly share code, notes, and snippets.

@sdaclin
Created January 18, 2019 15:07
Show Gist options
  • Save sdaclin/2b752ae8c991750a75f6187161e34911 to your computer and use it in GitHub Desktop.
Save sdaclin/2b752ae8c991750a75f6187161e34911 to your computer and use it in GitHub Desktop.
Will find and print recursively duplicated files in some dirs (comparison based on fileName + Size)
package fr.sdaclin.dedup
import java.io.File
/**
* Will find recursively duplicated files in some dirs (comparison based on fileName + Size)
*/
fun main(args: Array<String>) {
println("I'm working...")
findDuplicates(File("/firstDir"), File("/optionalSecondDir"))
.forEach {
val listDuplicated = it.value.joinToString(prefix = "\n\t", separator = "\n\t") { it.file.absolutePath }
println("${it.key}" + listDuplicated)
}
}
fun findDuplicates(vararg dirToProceed: File): Map<String, List<DupFile>> {
return dirToProceed.fold(emptySequence<File>()) { acc, file ->
acc + file
.walkTopDown()
.filter { it.isFile }
}
.map { DupFile(it, it.name, it.length()) }
.groupBy { it.name + "-" + it.size }
.filter { it.value.size > 1 }
}
class DupFile(val file: File, val name: String, val size: Long)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment