Find all files named "findthis.csv" in nested subfolders of "rootfolder"

I am trying to do what the title says and collect the file paths into a vector but having trouble. I would use find rootfolder -name findthis.csv in bash. readdir can only read one level deep, so I am trying to find a solution with walkdir. I am not understanding the provided examples. Why is the same command run sequentially yielding different results?

julia> mkpath("my/test/dir");

julia> itr = walkdir("my");

julia> (root, dirs, files) = first(itr)
("my", ["test"], String[])

julia> (root, dirs, files) = first(itr)
("my/test", ["dir"], String[])

julia> (root, dirs, files) = first(itr)
("my/test/dir", String[], String[])
julia> itr = 1:4
1:4

julia> first(itr)
1

julia> first(itr)
1

walkdir returns a Channel as the iterator which is a mutable iterator. The docstring should probably mention this.

If something is being mutated, then I would expect to see that indicated by the function: first!

I did see that but hoped there might be something simpler if I already know the exact file name.

It’s just how a Channel works:

julia> chnl = Channel() do ch
                    foreach(i -> put!(ch, i), 1:5)
                end
Channel{Any}(0) (1 item available)

julia> first(chnl)
1

julia> first(chnl)
2

julia> first(chnl)
3

julia> first(chnl)
4

The docstring for walkdir just fails to warn about it being implemented as a Channel. Could be a good issue.

Not an answer to why but might take you some of the way to a solution of the title question.

[root for (root, dirs, files) in walkdir(".") if filename in files]
2 Likes

PR for the docstring created here: Improve walkdir docstring by nathanrboyer · Pull Request #55476 · JuliaLang/julia (github.com)

1 Like

Please check this code adapted from @GunnarFarneback’s snippet above:

findfile(directory, file) = [joinpath(root, file) for (root, dirs, files) in walkdir(directory) if file in files]
1 Like

It looks like I/O objects may be mutated without indicating such with !. Though again, I wouldn’t necessarily realize I was creating an I/O object just to read some directory names, so it still strikes me as odd that first continues iterating the object.