Using FFileManagerGeneric for listing files in directory

#BP Node Solution

To answer the original post, the parameter is a misleading name, really this is not a filename, but a directory + a filename filter

so the “filename” is really is FullDirectoryPath/*.EXT

#BP Node For you!

Here’s a BP node that fully encapsulates how you can use this core UE4 functionality with an optional file extension filter!

/** Obtain all files in a provided directory, with optional extension filter. All files are returned if Ext is left blank. Returns false if operation could not occur. */
UFUNCTION(BlueprintPure, Category = "VictoryBPLibrary|File IO")
static bool JoyFileIO_GetFiles(TArray<FString>& Files, FString RootFolderFullPath, FString Ext);

bool UVictoryBPFunctionLibrary::JoyFileIO_GetFiles(TArray<FString>& Files, FString RootFolderFullPath, FString Ext)
{
	if(RootFolderFullPath.Len() < 1) return false;
	
	FPaths::NormalizeDirectoryName(RootFolderFullPath);
	
	IFileManager& FileManager = IFileManager::Get();
	
	if(Ext == "") 
	{
		Ext = "*.*";
	}
	else
	{
		Ext = (Ext.Left(1) == ".") ? "*" + Ext : "*." + Ext;
	}
	
	FString FinalPath = RootFolderFullPath + "/" + Ext;
	FileManager.FindFiles(Files, *FinalPath, true, false);
	return true;				  
}

#File Manager by Ref

I can highly recommend getting the File Manager by reference instead of making a new one via constructor :slight_smile:

IFileManager& FileManager = IFileManager::Get();

#Constructing the Final Search Filter

Notice how I combine the root directory with the optional filter of a file extension or wildcard:

FString FinalPath = RootFolderFullPath + "/" + Ext;
FileManager.FindFiles(Files, *FinalPath, true, false);

I do think this parameter should be renamed in FFileManagerGeneric :slight_smile:

void FindFiles( TArray& Result, const TCHAR Filename*, bool Files, bool Directories ) override;

void FindFiles( TArray& Result, const TCHAR Filter*, bool Files, bool Directories ) override;

#UE4 API Solution

Or, break up the parameter as I do in my BP node

void FindFiles( TArray<FString>& Result, const TCHAR* Directory, const TCHAR* Extension,  bool Files, bool Directories ) override;

#Enjoy!

#:heart:

Rama

1 Like