Skip to content

Instantly share code, notes, and snippets.

@PranavSK
Forked from hiulit/get_all_files.gd
Created August 11, 2022 08:47
Show Gist options
  • Save PranavSK/52c422e46bee934e1b7e8b7550e1d734 to your computer and use it in GitHub Desktop.
Save PranavSK/52c422e46bee934e1b7e8b7550e1d734 to your computer and use it in GitHub Desktop.
func get_all_files(path: String, file_ext := "", files := []):
var dir = Directory.new()
if dir.open(path) == OK:
dir.list_dir_begin(true, true)
var file_name = dir.get_next()
while file_name != "":
if dir.current_is_dir():
files = get_all_files(dir.get_current_dir().plus_file(file_name), file_ext, files)
else:
if file_ext and file_name.get_extension() != file_ext:
file_name = dir.get_next()
continue
files.append(file_name)
file_name = dir.get_next()
else:
print("An error occurred when trying to access %s." % path)
return files
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment