Skip to content

Instantly share code, notes, and snippets.

@superjohan
Created February 24, 2019 15:56
Show Gist options
  • Save superjohan/7abfde853e183a32c3c3a05ead2328ae to your computer and use it in GitHub Desktop.
Save superjohan/7abfde853e183a32c3c3a05ead2328ae to your computer and use it in GitHub Desktop.
Really simple somewhat functional filter for getting rid of duplicates in ROM sets
import java.io.File
fun main(args: Array<String>) {
val files = File("/path/to/roms/Game Boy").listFiles()
files.sortBy { it.name }
val gameNames = mutableListOf<String>()
for (file in files) {
val gameName = file.name.substringBefore(" (")
if (!gameNames.contains(gameName)) {
gameNames.add(gameName)
}
}
val okRoms = mutableListOf<File>()
for (name in gameNames) {
val romsForName = mutableListOf<File>()
for (file in files) {
val gameName = file.name.substringBefore(" (")
if (name == gameName) {
romsForName.add(file)
}
}
if (romsForName.count() == 1) {
okRoms.add(romsForName[0])
continue
}
val noBrackets = romsForName.filter { !it.name.contains("[")}
if (noBrackets.count() > 0) {
okRoms.addAll(noBrackets)
continue
}
val exclamationPoint = romsForName.filter { it.name.contains("[!]") }
if (exclamationPoint.count() > 0) {
okRoms.addAll(exclamationPoint)
continue
}
okRoms.add(romsForName[0])
}
okRoms.sortBy { it.name }
for (file in files) {
if (!okRoms.contains(file)) {
file.delete()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment