继承UBlueprintFunctionLibrary
.h
- //查找文件
- UFUNCTION(BlueprintCallable, Category = "ExtendedContent|File")
- static TArray<FString> FindFiles(FString Path, FString Filter, bool InFiles, bool InDirectory);//3true返回文件,4true返回文件夹
- /**移动文件位置*/
- UFUNCTION(BlueprintCallable, Category = "<span style="font-family:Arial, Helvetica, sans-serif;">ExtendedContent</span>|file")
- static bool MoveFileTo(FString To, FString From);
- /**删除一个文件*/
- UFUNCTION(BlueprintCallable, Category = "<span style="font-family:Arial, Helvetica, sans-serif;">ExtendedContent</span><span style="font-family:Arial, Helvetica, sans-serif;">|file")</span>
- static bool DeleteFile(FString FilePath);
.cpp
- TArray<FString> ULoadTexture::FindFiles(FString Path, FString Filter, bool InFiles, bool InDirectory)
- {
- TArray<FString> FilePathList;
- //清空数组
- FilePathList.Empty();
- //查找文件并将结果赋给FileList
- FFileManagerGeneric::Get().FindFilesRecursive(FilePathList, *Path, *Filter, InFiles, InDirectory);
- //返回结果
- return FilePathList;
- }
- bool ACPassword::MoveFileTo(FString To, FString From)
- {
- return IFileManager::Get().Move(*To, *From);
- }
- bool ACPassword::DeleteFile(FString FilePath)
- {
- return IFileManager::Get().Delete(*FilePath);
- }
创建文件夹
- bool AMyActor::CreateMyDirectory(const FString& TestDir)
- {
- // Every function call, unless the function is inline, adds a small
- // overhead which we can avoid by creating a local variable like so.
- // But beware of making every function inline!
- IPlatformFile& PlatformFile = FPlatformFileManager::Get().GetPlatformFile();
- // Directory Exists?
- if (!PlatformFile.DirectoryExists(*TestDir))
- {
- PlatformFile.CreateDirectory(*TestDir);
- return true;
- }
- else
- {
- GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Yellow, TEXT("File存在!"));
- return false;
- }
- }
使窗口可拖拽文件
#include "AllowWindowsPlatformTypes.h"
#include "shellapi.h"
#include "HideWindowsPlatformTypes.h"
- HWND hwndd = GetActiveWindow();
- DragAcceptFiles(hwndd, true);
获取文件大小
- FString AMyActor::GetMyFileSize(FString MyFilePath)
- {
- if (!FPlatformFileManager::Get().GetPlatformFile().FileExists(*MyFilePath))
- {
- GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Yellow, TEXT("Could Not Find File!"));
- return "0";
- }
- const int64 FileSize = FPlatformFileManager::Get().GetPlatformFile().FileSize(*MyFilePath);
- FString Message = FString::Printf(TEXT("File size is: %d"), FileSize);
- return Message;
- }
例:查找指定目录下的所有文件
- // 遍历文件夹下指定类型文件
- // Files 保存遍例到的所有文件
- // FilePath 文件夹路径 如 "D:\\MyCodes\\LearnUE4Cpp\\Source\\LearnUE4Cpp\\"
- // Extension 扩展名(文件类型) 如 "*.cpp"
- void UMyBlueprintFunctionLibrary::ScanDirectory(TArray<FString>& Files, const FString & FilePath, const FString& Extension)
- {
- FString SearchedFiles = FilePath + Extension;
- TArray<FString> FindedFiles;
- IFileManager::Get().FindFiles(FindedFiles, *SearchedFiles, true, false);
- FString SearchFile = "";
- for (int i = 0; i < FindedFiles.Num(); i++)
- {
- SearchFile = FilePath + FindedFiles[i];
- Files.Add(SearchFile);
- GEngine->AddOnScreenDebugMessage(-1, 100, FColor::Red, SearchFile);
- }
- }//参考:http://blog.youkuaiyun.com/xi_niuniu/article/details/54428027
- TArray<FString> ALoadPackageMapAsyncActor::GetAllFilesInDirector(const FString directory, const FString onlyFilesStartingWith, const FString extension)
- {
- FString Directory1 = FPaths::GameDir() + directory;
- TArray<FString> directoriesToSkip;
- IPlatformFile &PlatformFile = FPlatformFileManager::Get().GetPlatformFile();
- FLocalTimestampDirectoryVisitor Visitor(PlatformFile, directoriesToSkip, directoriesToSkip, false);
- PlatformFile.IterateDirectory(*Directory1, 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);
- const FString fileBaseName = FPaths::GetBaseFilename(filePath);
- bool shouldAddFile = true;
- if (!onlyFilesStartingWith.IsEmpty())
- {
- const FString left = fileName.Left(onlyFilesStartingWith.Len());
- if (!(fileName.Left(onlyFilesStartingWith.Len()).Equals(onlyFilesStartingWith)))
- shouldAddFile = false;
- }
- if (!extension.IsEmpty())
- if (!(FPaths::GetExtension(fileName, false).Equals(extension, ESearchCase::IgnoreCase)))
- shouldAddFile = false;
- if (shouldAddFile)
- files.Add(filePath);
- }
- if (files.Num() == 0)
- {
- GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, TEXT("no array"));
- }
- return files;
- }//参考:http://blog.youkuaiyun.com/u012803165/article/details/79034176