Created
June 18, 2016 22:08
-
-
Save igormukhin/ed86eec8c18958ffa110f0674123df5c to your computer and use it in GitHub Desktop.
This file contains 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 characters
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