Using FFileManagerGeneric for listing files in directory

Hello there

I think that using Engine builtin FFileManagerGeneric class is more accurate, than inventing wheel or using 3rd libraries.

I just want to list files from directory (where is project filesystem sandbox should be?)

I’ve looked at FindFiles method and it doesn’t helped me :stuck_out_tongue:

void FindFiles
(
    TArray < FString > & FileNames,
    const TCHAR * Filename,
    bool Files,
    bool Directories
)

I see from source code that Filename could be wildcard string, and that is great, but I’d like to set base directory (relative or absolute), but there is no such option.
Also I see SetSandboxEnabled method, but how to setup and where it is by default?

Don’t give up though, waiting for help

1 Like

Got it!

[absolute-path]\\[wildcard]

Where wildcard could be “" or ".ext” (without quotes)

Though want to explanation about sandbox

could you please share more code, how to get file list from the path? I didn’t get the logic.

Sure,

TArray<FString> FileNames;
FFileManagerGeneric FileMgr;
FileMgr.SetSandboxEnabled(true);// don't ask why, I don't know :P
FString wildcard("*.xml"); // May be "" (empty string) to search all files
FString search_path(FPaths::Combine(*FPaths::GameDir(), TEXT("Data"), *wildcard));

FileMgr.FindFiles(FileNames, *search_path, 
                                 true,  // to list files
                                 false); // to skip directories

for (auto f : FileNames)
{
        FString filename(f);
        f.RemoveFromEnd(".xml");
        OutputDebugStringA(TCHAR_TO_ANSI(*(f+"\n")));
}

FileNames.Empty();// Clear array

#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

Is it possible to add the full .cpp and .h for us blueprint users? =)

Also, I realize this was a very long time ago and may not work the same