Is there a better way to glob files in Kotlin than...
# announcements
n
Is there a better way to glob files in Kotlin than this snippet I found by googling?
Copy code
//Object a matcher object from the supplied Glob pattern
    val matcher = FileSystems.getDefault().getPathMatcher(glob)
 
    val path = Paths.get(start)
    //Walk the file system
    Files.walk(path)
            //Filter out anything that doesn't match the glob
            .filter { it : Path? -> it?.let { matcher.matches(it.fileName) } ?: false }
            //Collect to a list
            .collect(toList())
Not only is this code ugly, but it also seems incredibly inefficient to walk every file and filter, compared to how globs can actually be done. I don't have much faith in this snippet anyhow because there's some oddities but it's the best I'd found.
106 Views