Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

walkdir: iterates over contents of directory in pre- post- or breadth-first order #1765

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions base/file.jl
Original file line number Diff line number Diff line change
Expand Up @@ -317,3 +317,46 @@ end

readdir(cmd::Cmd) = readdir(string(cmd)[2:end-1])
readdir() = readdir(".")

# YouTests for this function available at https://gist.github.com/4294860
export walkdir
function walkdir(startdir, order)
function pre(dirname)
produce(dirname)
for filename in readdir(dirname)
fullname = file_path(dirname, filename)
if isdir(fullname)
pre(fullname)
else
produce(fullname)
end
end
end
function post(dirname)
for filename in readdir(dirname)
fullname = file_path(dirname, filename)
if isdir(fullname)
post(fullname)
else
produce(fullname)
end
end
produce(dirname)
end
function breadth(dirname)
q = {}
push(q, dirname)
produce(dirname)
while length(q) > 0
dirname = shift(q)
for filename in readdir(dirname)
fullname = file_path(dirname, filename)
if isdir(fullname) push(q, fullname) end
produce(fullname)
end
end
end
fn = {:pre => pre, :post => post, :breadth => breadth}[order]
@task fn(abs_path(startdir))
end
walkdir(startdir) = walkdir(startdir, :pre)