How to get folders inside of a folder?

Hello,

Just as I can use the next function to find all available files inside of a folder.

bool UMusicalRangeBPFunctionLibrary::LoadFromFileGetAvailableFiles(TArray<FString>& Files, FString FullFilePath) {
	FPaths::NormalizeDirectoryName(FullFilePath);
	IFileManager& FileManager = IFileManager::Get();

	FString FinalPath = FullFilePath + "/";
	FileManager.FindFiles(Files, *FinalPath, NULL);

	for (int i = 0; i < Files.Num(); i++)
	{
		UE_LOG(LogMusicalRange, Log, TEXT("File Found: %s"), *Files[i]);
	}
	return true;
}

How can I do something similar to find the folders inside of a folder? So, for example, I have a folder named “CustomSongs”, and I want to be able to get the name of all the folders inside of the folder CustomSongs.

Cheers!

1 Like

It seems that the function you’re already using (IFileManager::FindFiles) has overloads that cater for directories.

I suppose the following should work, but can’t test right now:

FileManager.FindFiles(Files, *FinalPath, false, true);

I just tried this out like this.

bool UMusicalRangeBPFunctionLibrary::LoadFromFileGetAvailableFolders(TArray<FString>& Folders, FString FullFilePath, bool printFolders) {
	FPaths::NormalizeDirectoryName(FullFilePath);
	IFileManager& FileManager = IFileManager::Get();

	FString FinalPath = FullFilePath + "/";
	FileManager.FindFiles(Folders, *FinalPath, false, true);

	if (printFolders)
	{
		for (int i = 0; i < Folders.Num(); i++)
		{
			UE_LOG(LogMusicalRange, Log, TEXT("File Found: %s"), *Folders[i]);
		}
	}
	return true;
}

Blueprint returned an empty array.

http://puu.sh/vgQlP/6213cb2767.jpg

And the folder in question I am searching.

http://puu.sh/vgQoH/e8d3756789.png

Not sure why it won’t work. Have you tried IterateDirectory?

So I just tried the following code.

bool UMusicalRangeBPFunctionLibrary::LoadFromFileGetAvailableFolders(TArray<FString>& Folders, FString FullFilePath, bool printFolders) {
	FPaths::NormalizeDirectoryName(FullFilePath);
	IFileManager& FileManager = IFileManager::Get();

	FileManager.FindFiles(Folders, *FullFilePath, true, true);

	if (printFolders)
	{
		for (int i = 0; i < Folders.Num(); i++)
		{
			UE_LOG(LogMusicalRange, Log, TEXT("File Found: %s"), *Folders[i]);
		}
	}
	return true;
}

And it fails to load anything. No extensions, no files within, it seems to return the last folder in /foldername/.

http://puu.sh/vjmCe/9da494a386.png

And the actual folder in Windows Explorer

http://puu.sh/vjmEt/81257341a2.png

The path you send is treated as a specific filename unless you include a wildcard character.

Try:

FString FinalPath = FullFilePath / TEXT("*");
FileManager.FindFiles(Folders, *FinalPath, false, true);

Also, I haven’t tested this, but the documentation for FindFiles says “files or directories” rather than “files and/or directories” so you may not be able to pass “true” to both. I’d be a bit surprised by that, but if you still have trouble it’s worth a shot just doing one or the other.

1 Like

This did it!

Thanks! Didn’t know I had to include a " * " and was having trouble.