Download

How do I "scan" a path for it's subdirectories?

Hey everyone!
I’m currently developing a plugin which can load/save data as folder-based structures (Like a User-Profile) and I need to check which profiles are currently there :o
I thought the best way of doing it is to create a function that returns a TArray of all Subfolders being found.
I already tried using the FPlatformFileManager::IterateDirectory however I can’t use this since it’s inhereting from it’s own class but I need to use variables in my handler class.
Is there another way of doing it?

You can write class derived from FDirectoryVisitor and pass whatever you want.

class FFileFinder : public FDirectoryVisitor
{
  private:
    TArray<FString> Files;
    FString Extension;
  public:
    explicit FFileFinder(FString const& InExtension) : Extension{ InExtension }
  {}
    TArray<FString> GetFiles() { return Files; }
    TArray<FString> const& GetFiles() const { return Files; }
    bool Visit(TCHAR const* Name, bool bIsDirectory) override
    {
      if (!bIsDirectory && FPaths::GetExtension(Name, false) == Extension)) {
        Files.Emplace(Name);
      }
      return true;
    }
};

// using
TArray<FString> FSomething::ListPngFiles(FString const& BaseDir)
{
  auto Finder = FFileFinder{ "png" };
  if (FPlatformFileManager::Get().GetPlatformFile()->IterateDirectory(*BaseDir, Finder)) {
    return Finder.GetFiles();
  }
  return {};
}