Task

Walk a given directory and print the names of files matching a given pattern.

(How is "pattern" defined? substring match? DOS pattern? BASH pattern? ZSH pattern? Perl regular expression?)


Note: This task is for non-recursive methods.   These tasks should read a single directory, not an entire directory tree.

Note: Please be careful when running any code presented here.



Related task

   Walk Directory Tree   (read entire directory tree). 


// version 1.1.2
 
import java.io.File
 
fun walkDirectory(dirPath: String, pattern: Regex): List<String> {
    val d = File(dirPath)
    require(d.exists() && d.isDirectory())
    return d.list().filter { it.matches(pattern) }
}
 
fun main(args: Array<String>) {
    val r = Regex("""^a.*\.h$""")  // get all C header files beginning with 'a'
    val files = walkDirectory("/usr/include", r)
    for (file in files) println(file)
}