Skip to content

Instantly share code, notes, and snippets.

@igormukhin
Created June 18, 2016 22:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save igormukhin/ed86eec8c18958ffa110f0674123df5c to your computer and use it in GitHub Desktop.
Save igormukhin/ed86eec8c18958ffa110f0674123df5c to your computer and use it in GitHub Desktop.
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
import java.util.regex.Pattern
fun main(args: Array<String>) {
val filesSrcDir = Paths.get(args[0])
val renames: MutableMap<Path, String> = hashMapOf()
Files.walk(filesSrcDir)
.filter { Files.isRegularFile(it) }
.forEach { process(it, renames) }
renames.forEach { entry ->
val source = entry.key
val newName = entry.value
println("${source.toString()} -> $newName")
Files.move(source, source.resolveSibling(newName));
}
}
fun process(path: Path, renames: MutableMap<Path, String>) {
val pattern = Pattern.compile("\\#U(\\p{XDigit}\\p{XDigit}\\p{XDigit}\\p{XDigit})")
val matcher = pattern.matcher(path.fileName.toString())
val sb = StringBuffer()
while (matcher.find()){
val s = matcher.group(1)
val ch = Integer.parseInt(s, 16).toChar()
matcher.appendReplacement(sb, ch.toString())
}
matcher.appendTail(sb)
val oldName = path.fileName.toString()
val newName = sb.toString()
if (oldName != newName) {
renames.put(path, newName)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment