This language bar is your friend. Select your favorite languages!
Select your favorite languages :
- Or search :
string D = @"C:\The\Search\Path";
// Assumes that the file system is case insensitive
HashSet<string> exts = new HashSet<string>(StringComparer.CurrentCultureIgnoreCase) { "jpg", "jpeg", "png" };
IEnumerable<string> L =
Directory
.EnumerateFiles(D, "*", SearchOption.AllDirectories)
.Where(currentFile => exts.Contains(Path.GetExtension(currentFile)));
Enumerating all files under the given directory select any file whose extension exists in the HashSet (case insensitive using the current culture). If you want this to be case sensitive you can remove the comparer.
immutable exts = ["jpg", "jpeg", "png"];
auto L = D.dirEntries(SpanMode.depth)
.map!(to!string)
.filter!(f => exts.canFind(f.split(".")[$-1]));
dirEntries to list all files, convert them to string using map, then filter that list to keep only filenames whose extension is in exts. This is lazy.
var extensions = [".jpg", "jpeg", ".png"];
var L = Directory(D)
.listSync(recursive: true)
.where((file) => extensions.contains(p.extension(file.path)))
.toList();
L := []string{}
err := filepath.Walk(D, func(path string, info os.FileInfo, err error) error {
if err != nil {
fmt.Printf("failure accessing a path %q: %v\n", path, err)
return err
}
for _, ext := range []string{".jpg", ".jpeg", ".png"} {
if strings.HasSuffix(path, ext) {
L = append(L, path)
break
}
}
return nil
})
static List<String> L = new ArrayList<>();
record Find(File d, String ... t) {
Find { get(d, t); }
void get(File d, String ... t) {
File a[] = d.listFiles();
if (a == null) return;
for (File f : a)
if (f.isDirectory()) get(f, t);
else for (String s : t)
if (f.getName().endsWith('.' + s)) {
L.add(f.getAbsolutePath());
break;
}
}
}
filtered_files = ["{}/{}".format(dirpath, filename) for dirpath, _, filenames in os.walk(D) for filename in filenames if re.match(r'^.*\.(?:jpg|jpeg|png)$', filename)]
* list comprehension
* iterate over all files and all directories in tree under D (os module)
* iterate over all files found
* filter with regex matching the end of the filename (re module)
* regex is cached, but may be compiled beforehands
* iterate over all files and all directories in tree under D (os module)
* iterate over all files found
* filter with regex matching the end of the filename (re module)
* regex is cached, but may be compiled beforehands
let d = Path::new("/path/to/D");
let l: Vec<PathBuf> = d
.read_dir()?
.filter_map(|f| f.ok())
.filter(|f| match f.path().extension() {
None => false,
Some(ex) => ex == "jpg" || ex == "jpeg" || ex == "png"
})
.map(|f| f.path())
.collect();
The _? operator simplifies error handling. See https://doc.rust-lang.org/rust-by-example/std/result/question_mark.html