How to get file list in a directory?

I want to list all the file name in a directory, Is there any function available in unreal?

Nope. That kind of behavior would never be necessary for a game engine. At a guess, the closest you might get would be the engine’s save game system but that functionality is extremely specific (and limited).

Indeed but I’m not trying to encourage nefarious behavior :wink: I should have quantified by adding “You could always write your own functionality for that.”

Adding to the above comment … you do have access to the Source code and you are posting in C++ programming, so you could add your own code to do this.

How you approach this would depend on your skillset and you might end up breaking the support for Linux if you use Windows specific calls.

Let’s say I have more than 5 files(such as: Shape_Cone.uasset, Shape_Cube.uasset, and many more) in folder(/Game/StarterContent/Shapes/).
I’d like get the all file names and store in a FString array. Is there any way to get this logic working?

Short answer: Potentially / It depends.

Caveat: You’d have to write that functionality yourself in C++ and include it in the engine recompile.

I did this a few weeks back. Please note that I’m using Windows, so it might not work on other platforms.
In any case, I use the built-in Unreal functionality as far as possible, and I think most of it is platform-independent.

/** 
Gets all the files in a given directory.
@param directory The full path of the directory we want to iterate over.
@param fullpath Whether the returned list should be the full file paths or just the filenames.
@param onlyFilesStartingWith Will only return filenames starting with this string. Also applies onlyFilesEndingWith if specified.
@param onlyFilesEndingWith Will only return filenames ending with this string (it looks at the extension as well!). Also applies onlyFilesStartingWith if specified.
@return A list of files (including the extension).
*/
TArray<FString> UPWNGameGlobals::GetAllFilesInDirectory(const FString directory, const bool fullPath, const FString onlyFilesStartingWith, const FString onlyFilesWithExtension)
{
	// Get all files in directory
	TArray<FString> directoriesToSkip;
	IPlatformFile &PlatformFile = FPlatformFileManager::Get().GetPlatformFile();
	FLocalTimestampDirectoryVisitor Visitor(PlatformFile, directoriesToSkip, directoriesToSkip, false);	
	PlatformFile.IterateDirectory(*directory, Visitor);
	TArray<FString> files;

	for (TMap<FString, FDateTime>::TIterator TimestampIt(Visitor.FileTimes); TimestampIt; ++TimestampIt)
	{
		const FString filePath = TimestampIt.Key();		
		const FString fileName = FPaths::GetCleanFilename(filePath);
		bool shouldAddFile = true;

		// Check if filename starts with required characters
		if (!onlyFilesStartingWith.IsEmpty())
		{
			const FString left = fileName.Left(onlyFilesStartingWith.Len());			

			if (!(fileName.Left(onlyFilesStartingWith.Len()).Equals(onlyFilesStartingWith)))
				shouldAddFile = false;
		}

		// Check if file extension is required characters
		if (!onlyFilesWithExtension.IsEmpty())
		{
			if (!(FPaths::GetExtension(fileName, false).Equals(onlyFilesWithExtension, ESearchCase::IgnoreCase)))
				shouldAddFile = false;
		}

		// Add full path to results
		if (shouldAddFile)
		{
			files.Add(fullPath ? filePath : fileName);
		}
	}

	return files;
}

You can use it as follows:

FString directoryToSearch = TEXT("c:/YourGame/YourDir");
FString filesStartingWith = TEXT("");
FString fileExtensions = TEXT("sav");

TArray filesInDirectory = GetAllFilesInDirectory(directoryToSearch, true, filesStartingWith, fileExtensions);
2 Likes

P.S. Three more things:

  1. You can declare it in you header file as follows:

    static TArray GetAllFilesInDirectory(const FString directory, const bool fullPath = true, const FString onlyFilesStartingWith = TEXT(""), const FString onlyFilesEndingWith = TEXT(""));

  2. In the .cpp file, you will obviously need to replace “UPWNGameGlobals” with the name of the class you are defining this function in.

  3. I use a combination of native directories and “FPaths::Combine()” to maintain platform independence. E.g.

    FString MySavedGamePath = FPaths::Combine(FPaths::GameUserDir(), TEXT(“SavedGames”));

Thanks a lot. This solves for my problem