UE4 获取本地文件以及修改

1.获取本地文件夹以及文件

//AMyActor_a.h
    //Filepath指定的文件路径
    //Extension扩展名如(*.h)
    UFUNCTION(BlueprintCallable, Category = "GetFildContent")
		static bool GetFileContent(const FString & Filepath,const FString & Extension);
---------------------------------------------------------------------------------------
//AMyActor_a.cpp
bool AMyActor_a::GetFileContent(const FString & Filepath, const FString & Extension)
{
	//文件夹路径
	TArrayDirsArray;
	FString SearchedFiles = Filepath + "/" + TEXT("*");
	//返回Filepath该路径下的文件夹
	IFileManager::Get().FindFiles(DirsArray, *SearchedFiles, false, true);
	//返回文件
	//IFileManager::Get().FindFiles(DirsArray, *SearchedFiles, true, false);
	for (size_t i = 0; i < DirsArray.Num(); i++)
	{
		FString filePath = Filepath + "/" + DirsArray[i] + "/";
		FString fileWithExtension = filePath + Extension;
		UE_LOG(LogTemp, Error, TEXT("TARRAY____%s"), *DirsArray[i]);
		UE_LOG(LogTemp, Error, TEXT("文件夹____%s"), *fileWithExtension);
		TArray filesArray;
		IFileManager::Get().FindFiles(filesArray, *fileWithExtension, true, false);
		for (size_t j = 0; i < filesArray.Num(); i++)
		{
			FString fileFullName = filePath + filesArray[j];
			UE_LOG(LogClass, Error, TEXT("旧文件名...%s"), *fileFullName);
		}
	}
	return true;
}
————————————

如图:

UE4 获取本地文件以及修改_第1张图片

UE4 获取本地文件以及修改_第2张图片

结果:


2.修改本地文件名称


AModifyActor.h

UFUNCTION(BlueprintCallable, Category = "UModifyFileName|File")
		static TArray ModifyFilesNameByID(const FString& FilePath, const FString& Extension, int PlanID);
------------------------------------------------------------------------------------------------------------------
AModifyActor.cpp

TArray AModifyActor::ModifyFilesNameByID(const FString& FilePath, const FString& Extension, int PlanID)
{
	FString SearchedFiles = FilePath;
	UE_LOG(LogClass, Error, TEXT("路径...%s"), *SearchedFiles);
	TArray DirsArray;

	FString path = SearchedFiles + "/" + TEXT("*");
	IFileManager& fileMgr = IFileManager::Get();
	//查找目录
	fileMgr.FindFiles(DirsArray, *path, false, true);
	for (size_t i = 0; i < DirsArray.Num(); i++)
	{
		TArray filesArray;
		FString filePath = FilePath + "/" + DirsArray[i] + "/";//文件路径
		FString fileWithExtension = filePath + Extension;
		UE_LOG(LogClass, Error, TEXT("文件路径...%s"), *fileWithExtension);

		//查找文件
		fileMgr.FindFiles(filesArray, *fileWithExtension, true, false);

		for (size_t j = 0; j < filesArray.Num(); j++)
		{
			FString fileFullName = filePath + filesArray[j];
			FString oldFileName = fileFullName;
			FString nameTemp = fileFullName;
			FString* leftString = new FString();
			FString* rightString = new FString();
			//拆分
			nameTemp.Split(".", leftString, rightString, ESearchCase::CaseSensitive, ESearchDir::FromStart);
			FString pid = FString::FromInt(PlanID);
			FString newFlieName = *leftString + "_" + pid + "." + *rightString;


			std::string oldName_Cstr(TCHAR_TO_UTF8(*oldFileName));
			const char * oldstr = oldName_Cstr.c_str();

			std::string newName_Cstr(TCHAR_TO_UTF8(*newFlieName));
			const char * newstr = newName_Cstr.c_str();
			UE_LOG(LogClass, Error, TEXT("旧文件名...%s"), *oldFileName);
			UE_LOG(LogClass, Error, TEXT("新文件名...%s"), *newFlieName);
            //修改要修改的路径 oldstr原始路径,newstr将要修改的路径
			rename(oldstr, newstr);
		}
	}

	return DirsArray;
}//获取当前项目的save文件夹路径
FPaths::GameSavedDir(): {return FString val}




你可能感兴趣的:(获取以及修改本地文件)