Is there a way to return the total number of files in a folder? I don't need to know what these files are or what they contain, but just how many are in the folder. Thanks
Get total number of files in a folder
You would have to count them. It's not difficult.
No it does not appear that function is supported. You could do it with native OS calls, but you'd have to support each platform you export to.
Wait, there is a way, but you have to do it manually.
func list_files_in_directory(path):
var files = []
var dir = Directory.new()
dir.open(path)
dir.list_dir_begin()
while true:
var file = dir.get_next()
if file == "":
break
elif not file.begins_with("."):
files.append(file)
dir.list_dir_end()
return files
- Edited
cybereality
If you wanted to get hidden files in linux systems as well, I believe you have to replace:
elif not file.begins_with(".")
with:
elif file != ".." && file != "."
I am on day 5 with no 5hr+ sleep, it's very likely I'm missing something obvious.
(you could do and
instead of &&
. i prefer the &&
)
I didn't write this code. I just put "Godot find all files in folder" into Google and this was the first hit.
https://ask.godotengine.org/5175/how-to-get-all-the-files-inside-a-folder
cybereality I should have figured. I knew that code looked familiar. Besides that, didn't you say you never use while true
?
Correct. I would have coded it recursively.
It says that directory is not declared in the current scope. It asks if I meant DirAccess so I put that in instead of Directory but then it can't be constructed because it is abstract
- Edited
Anson558 DirAccess
You're evidently using Godot 4. There's an example on how to iterate through the files of a directory here:
https://docs.godotengine.org/en/4.0/classes/class_diraccess.html#diraccess