Download

InterateDirectory Questions

Hello,

I have been searching the internet, the forums, the wiki, and more. I can’t seem to find any information on IterateDirectory using the FPlatformFileManager. I have had success with checking if a directory exists using the manager but I do not know how to use the IterateDirectory function. What is a Visitor? How do I use it? I will admit c++ is rather new to me but normally there is a wealth of knowledge on the internet. I appreciate any help. This is the definition and below it is my code so far:

/** 
 * Call the Visit function of the visitor once for each file or directory in a single directory. This function does not explore subdirectories.
 * @param Directory		The directory to iterate the contents of.
 * @param Visitor		        Visitor to call for each element of the directory
 * @return				false if the directory did not exist or if the visitor returned false.
**/

TArray<FString> UMyFileManager::GetFilePathsFromDirectory(FString PathToDirectory)
{
	
	// Array of fstrings to pass back
	TArray<FString> filePaths;
		
	// Check if the directory supplied exists
	if ( FPlatformFileManager::Get().GetPlatformFile().DirectoryExists( *PathToDirectory ) )
	{
		// Directory exists so loop through the files
		FPlatformFileManager::Get().GetPlatformFile().IterateDirectory(*PathToDirectory, myVisitor);
	}

	return filePaths;

}

You need to subclass IPlatformFile::FDirectoryVisitor and override the Visit() method that will be called for every file/directory found in the search path. Then you create an instance of your visitor class (usually on the stack) and pass that through to IterateDirectory() along with the path to the directory where the search should begin. There are multiple examples of this in the engine source, so if you haven’t done so yet grab the source and familiarize yourself with the code searching/navigation features of your IDE of choice, the engine source is an extremely useful resource.

I seriously did not even think to search the source code. Thank you so much for the tip. You have given me a good place to start!

v/r

Josh

So I had a look at the source and saw some spots it is used. Here is what I cam up with. Hopefully someone else can use this as guidance. I seriously looked for days through the internet. Of course if I would have looked in the source I would have found it in a day lol. Thanks again for the help.

.h


// Class used for the visitor in iterate directory
class FFileMatch : public IPlatformFile::FDirectoryVisitor
{
public:
	TArray<FString>& Result;
	FString WildCard;
	bool bFiles;
	bool bDirectories;
	FFileMatch(TArray<FString>& InResult, const FString& InWildCard, bool bInFiles, bool bInDirectories)
		: Result(InResult)
		, WildCard(InWildCard)
		, bFiles(bInFiles)
		, bDirectories(bInDirectories)
	{
	}

	virtual bool Visit(const TCHAR* FilenameOrDirectory, bool bIsDirectory);
	
};

.cpp


bool FFileMatch::Visit(const TCHAR* FilenameOrDirectory, bool bIsDirectory)
{
	
	// Pass back the file name
	new(Result)FString(FPaths::GetCleanFilename(FilenameOrDirectory));

	return true;
}

TArray<FString> UMyFileManager::GetFilePathsFromDirectory( FString PathToDirectory )
{

	// Array of fstrings to pass back
	TArray<FString> filePaths;

	// Reference to the FPlatformManager
	FPlatformFileManager* myFileManager = new FPlatformFileManager;

	// Reference to the FFileMatch class that has the visit override for the Visitor needed for iterate directory
	FFileMatch FileMatch( filePaths, FString(".dds"), true, false );
			
	// Check if the directory supplied exists
	if ( FPlatformFileManager::Get().GetPlatformFile().DirectoryExists( *PathToDirectory ) )
	{
		// Directory exists so loop through the files
		myFileManager->Get().GetPlatformFile().IterateDirectory(*PathToDirectory, FileMatch);

	}

	// Return the array
	return filePaths;

}