I am trying to write a program in Julia that given a starting folder, will loop through all sub folders such that I can open and get the contents out of all the files in the sub folders. How can I do this in Julia ?
Ideally the code would allow for an unspecified folder depth in case I don’t know it ahead of time.
A subfolder is a folder stored inside another folder. Subfolders help you organize your files more completely. Each subfolder should be used to store files related to each other. For example, you might have one folder for files related to a job search.
The command to change working directory is cd(dir::AbstractString=homedir()).
You can use walkdir
, like so:
for (root, dirs, files) in walkdir("mydir")
operate_on_files(joinpath.(root, files)) # files is a Vector{String}, can be empty
end
https://docs.julialang.org/en/v1/base/file/#Base.Filesystem.walkdir
Edit: A good thing to do here is to broadcast across the array of file paths, so that you don't need to special-case an empty array.
contents = String[]
for (root, dirs, files) in walkdir("mydir")
# global contents # if in REPL
push!.(Ref(contents), read.(joinpath.(root, files), String))
end
Maybe write a recursive function that lists all folders and files in the dir, pushes the contents of each file to a higher-scope Array, then calls itself on each of the folders? Sth like (untested code):
function outerfun(dir)
function innerfun!(dir, filecontents)
for name in readdir(dir)
if isdir(name)
innerfun!(name, filecontents)
else
push!(readlines(name), filecontents)
end
end
end
filecontents = Array{String}[]
innerfun!(dir, filecontents)
filecontents
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With