here is a recursive function to get all files from a nested directory. You can choose if the files should be included from the top directory you give to the function.
The function traverses each item in the given directory, if the item type is directory the function calls itself to traverse the subdirectory and merges the found files, else if the item type is a file it loads it and returns them as a dictionary {“file_path”: “file_ressource”}.
Make any edits as you like. eg. error catching for load(file_path), or if you want it to assign the files in an array instead etc.
func get_file(file_path):
return load(file_path)
func get_files_from_nested_directory(dir_path: String, incl_files_from_given_dir: bool = true):
var files = {}
var file
var item
var item_path
var dir := DirAccess.open(dir_path)
if dir == null: printerr("Could not open folder", dir_path); return
dir.list_dir_begin()
item = dir.get_next()
while item != "":
item_path = dir_path + "/" + item
if dir.dir_exists(item_path):
files.merge(get_files_from_nested_directory(item_path,true))
if dir.file_exists(item_path) and incl_files_from_given_dir:
files[item_path] = get_file(item_path)
item = dir.get_next()
dir.list_dir_end()
return files```