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). 


extern crate docopt;
extern crate regex;
extern crate rustc_serialize;
 
use docopt::Docopt;
use regex::Regex;
 
const USAGE: &'static str = "
Usage: rosetta <pattern>
 
Walks the directory tree starting with the current working directory and
print filenames matching <pattern>.
";
 
#[derive(Debug, RustcDecodable)]
struct Args {
    arg_pattern: String,
}
 
fn main() {
    let args: Args = Docopt::new(USAGE)
        .and_then(|d| d.decode())
        .unwrap_or_else(|e| e.exit());
 
    let re = Regex::new(&args.arg_pattern).unwrap();
    let paths = std::fs::read_dir(".").unwrap();
 
    for path in paths {
        let path = path.unwrap().path();
        let path = path.to_str().unwrap();
 
        if re.is_match(path) {
            println!("{}", path);
        }
    }
}