How to check if there are .file in the user://

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Mrpaolosarino

I want to make a load scene where every .file is represented in a box. Meaning I want to create a multiple load scene that I can just press and it load. I need to detect if there is .file with any name so I can use it

:bust_in_silhouette: Reply From: whiteshampoo

You can use this function:

func get_files_from_directory(path : String, suffix : String = "") -> PoolStringArray:
	var list : PoolStringArray = PoolStringArray()
	var dir : Directory =
	if not dir.dir_exists(path):
		push_warning("Path does not exist: " + path)
		return list
	var file : String = " "
	while file != "":
		file = dir.get_next()
		if not file:
			break # EOD
		if dir.current_is_dir():
			continue # Just files, no directories
		if suffix and not file.ends_with(suffix):
			continue # not the correct suffix
		list.append(path + file)
	return list

It will return an Array with all files that end with ‘.file’ in user:// if you use it like this:

var files : PoolStringArray = get_files_from_directory("user://", ".file")