Last active
June 13, 2021 06:18
-
-
Save universax/bb96882bb2e4d0c6396a104976bd4633 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
TArray<FString> GetAllFilesInDirectory(const FString directory, const bool fullPath, const bool withoutExtention, 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) | |
{ | |
if (withoutExtention) | |
files.Add(fullPath ? filePath : FPaths::GetBaseFilename(filePath)); | |
else | |
files.Add(fullPath ? filePath : FPaths::GetCleanFilename(filePath)); | |
} | |
} | |
return files; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment