Search for files in a folder

An alternative solution is to use the Glob.jl package. For example, if you have the following list of files in your directory:

foo1.txt
foo2.txt
foo3.txt
bar1.txt
foo.jl

and you want to find all text files starting with "foo" you would write

using Glob
glob("foo*.txt") #if searching the working directory
#output:
#"foo1.txt"
#"foo2.txt"
#"foo3.txt"
glob("foo*.txt","path/to/dir") #for specifying a different directory
#output:
#"path/to/dir/foo1.txt"
#"path/to/dir/foo2.txt"
#"path/to/dir/foo3.txt"

In Julia, the equivalent to list.files() is readdir([path])

There is no built-in directory search that I know of, but it is a one-liner:

searchdir(path,key) = filter(x->contains(x,key), readdir(path))


UPDATE: Since at least Julia v0.7, contains() has been deprecated for occursin(substring, string). So the above filter would now be:

searchdir(path,key) = filter(x->occursin(key,x), readdir(path))

Tags:

Julia