Created
March 16, 2021 16:41
-
-
Save sl5net/959773b588556b7f68000de75f1a0b0c to your computer and use it in GitHub Desktop.
adds information to folder name
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 characters
import java.io.File | |
import java.io.IOException | |
import java.nio.file.Files | |
import java.nio.file.Paths | |
import java.security.KeyStore | |
// TODO: somewhere in future: for json better us this: https://github.com/zogar1993/jsonkraken | |
fun main() { | |
val isDemoMode = true | |
val property = System.getProperty("user.home") | |
property.toString() | |
val d0 = "$property/snap/0ad/199/.local/share/0ad/replays/0.0.24"; | |
val d1 = "$d0/2021-03-14_0016"; | |
val f_dont_work = "$d0/2021-03-10_0011/commands.txt" | |
try { | |
var newFileName = readFileLineByLineUsingForEachLine2(f_dont_work) | |
println(newFileName) | |
} catch (ex: Exception) { | |
} | |
// val r = Regex("""^a.*\.h$""") // get all C header files beginning with 'a' | |
val r = Regex("""^[\d_\-]+$""") // get all C header files beginning with 'a' | |
val files = walkDirectory(d0, r) | |
for (d_fresh in files) { | |
val smallPrefix = d_fresh.subSequence(0, 15) | |
val fWithoutPath = "$d_fresh/commands.txt"; | |
val f = "$d0/$d_fresh/commands.txt"; | |
var file = File(f) | |
var fileExists = file.exists() | |
if (fileExists) { | |
try { | |
var newFileName = readFileLineByLineUsingForEachLine2(f) | |
println() | |
val dFrom = "$d0/$d_fresh" | |
println(dFrom) | |
val d2 = "$d0/$smallPrefix $newFileName"; | |
println("$d2") | |
if (!isDemoMode) | |
moveDirectory(dFrom, d2) | |
println() | |
} catch (ex: Exception) { | |
println(f) | |
} | |
} | |
} | |
}; | |
fun moveDirectory( | |
sourceFile: String, | |
targetFile: String, | |
) { | |
val sourceFilePath = Paths.get(sourceFile) | |
val targetFilePath = Paths.get(targetFile) | |
try { | |
Files.move(sourceFilePath, targetFilePath) | |
} catch (ex: FileAlreadyExistsException) { | |
println("Target file already exists") | |
} catch (ex: IOException) { | |
System.out.format("I/O error: %s%n", ex) | |
} | |
println("test") | |
} | |
// https://stackoverflow.com/a/66654983/2891692 : | |
fun readFileLineByLineUsingForEachLine2(fileName: String) = | |
File(fileName).useLines { lines -> | |
val (name, civ, name1, civ1) = lines | |
.map { | |
val regexString = """ | |
"Name":"(?<Name>[^"]+)".*?"Civ":"(?<Civ>\w+)".*?"Team":\-?\d+.*?"Name":"(?<Name1>[^"]+)".*?"Civ":"(?<Civ1>\w+)".*?"Team":\-?\d+ | |
""".trim() | |
var regex = Regex(regexString, RegexOption.IGNORE_CASE) | |
// var matched = regex.find(it)?.groupValues | |
regex.find(it) | |
} | |
.filterNotNull() | |
.first() | |
.destructured | |
name + "_" + civ + "_vs" + "_" + name1 + "_" + civ1 | |
// .First() will throw an exception if there's no row to be returned, while .FirstOrDefault() | |
} | |
fun walkDirectory(dirPath: String, pattern: Regex): List<String> { | |
val d = File(dirPath) | |
require(d.exists() && d.isDirectory()) | |
return d.list().filter { it.matches(pattern) } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment