TECHARTNOMAD | TECHARTFLOWIO.COM

TECH.ART.FLOW.IO

[Fork][Pipeline] File and Folder Management, Create, Find, Delete

jplee 2023. 10. 5. 00:38

 

역자의 말.
유니티 엔진만 너무 오래 사용해서 게임개발을 하다가 몇 년전부터 언리얼 엔진으로 프로젝트를 하기 시작 했습니다. 최근에는 유독 파이프라인 관련 된 일들도 종종 하게 되는데 파일 IO 등에 대해서 정리 된 기사가 있어서 간단히 번역해놨습니다.

원문.

 

File and Folder Management, Create, Find, Delete | Unreal Engine Community Wiki

Introduction In order to handle files and folders in Unreal Engine 4. And stay cross-platform you need to use their file system. The UFS(Unreal File System), Here I will show you how to use UFS to ...

unrealcommunity.wiki

언리얼 엔진 4에서 파일과 폴더를 처리하려면. 그리고 크로스 플랫폼을 유지하려면 파일 시스템을 사용해야 합니다. UFS(언리얼 파일 시스템), 여기서는 UFS 사용법을 보여드리겠습니다.

 

Introduction

언리얼 엔진 4에서 파일과 폴더를 처리하려면. 그리고 크로스 플랫폼을 유지하려면 해당 파일 시스템을 사용해야 합니다. 여기서는 UFS(언리얼 파일 시스템)를 사용하여 파일과 폴더 자체를 조작하는 방법을 보여드리겠습니다. 아쉽게도 파일 콘텐츠를 변경하는 방법은 보여주지 않겠습니다. 이 부분은 다른 글에서 다루겠습니다.

먼저 파일 또는 폴더의 올바른 경로를 찾는 방법을 살펴보겠습니다. 프로젝트 경로 또는 엔진 경로 찾기.

Paths

경로와 폴더를 다룰 때 가장 먼저 해야 할 일 중 하나는 올바른 경로 또는 폴더를 찾는 것입니다. 표준 UE4 폴더의 경우, 그 폴더로 가는 훌륭한 방법이 있습니다.
이제 프로젝트 디렉터리를 가져와 로그해 보겠습니다.

// Get the project directory and save it in ProjectDirectory
FString ProjectDirectory = FPaths::ProjectDir();
UE_LOG(LogTemp, Warning, TEXT("FilePaths: ProjectDirectory: %s"),*ProjectDirectory);

네! 이정도면 괜찮네요!

LogTemp: Warning: FilePaths: ProjectDirectory: D:/Projects/UE4/UFS/

내 프로젝트가 D:/Projects/UE4/UFS/에 있는 것을 확인할 수 있습니다.

 

이제 다른 폴더가 많이 있습니다. 살펴봅시다.

 

FString ProjectDirectory = FPaths::ProjectDir();
    UE_LOG(LogTemp, Warning, TEXT("FilePaths: ProjectDir: %s"),*ProjectDirectory);

    FString ProjectConfigDirectory = FPaths::ProjectConfigDir();
    UE_LOG(LogTemp, Warning, TEXT("FilePaths: ProjectConfigDir: %s"),*ProjectConfigDirectory);

    FString ProjectContentDirectory = FPaths::ProjectContentDir();
    UE_LOG(LogTemp, Warning, TEXT("FilePaths: ProjectContentDir: %s"),*ProjectContentDirectory);
    
    FString ProjectIntermediateDirectory = FPaths::ProjectIntermediateDir();
    UE_LOG(LogTemp, Warning, TEXT("FilePaths: ProjectIntermediateDirectory: %s"),*ProjectIntermediateDirectory);

    FString ProjectLogDirectory = FPaths::ProjectLogDir();
    UE_LOG(LogTemp, Warning, TEXT("FilePaths: ProjectLogDir: %s"),*ProjectLogDirectory);

    FString ProjectModsDirectory = FPaths::ProjectModsDir();
    UE_LOG(LogTemp, Warning, TEXT("FilePaths: ProjectModsDir: %s"),*ProjectModsDirectory);

    FString ProjectPluginsDirectory = FPaths::ProjectPluginsDir();
    UE_LOG(LogTemp, Warning, TEXT("FilePaths: ProjectPluginsDir: %s"),*ProjectPluginsDirectory);

    FString ProjectSavedDirectory = FPaths::ProjectSavedDir();
    UE_LOG(LogTemp, Warning, TEXT("FilePaths: ProjectSavedDir: %s"),*ProjectSavedDirectory);

    FString ProjectUserDirectory = FPaths::ProjectUserDir();
    UE_LOG(LogTemp, Warning, TEXT("FilePaths: ProjectUserDir: %s"),*ProjectUserDirectory);

    FString ProjectPersistentDownloadDirectory = FPaths::ProjectPersistentDownloadDir();
    UE_LOG(LogTemp, Warning, TEXT("FilePaths: ProjectPersistentDownloadDir: %s"),*ProjectPersistentDownloadDirectory);

    FString ProjectPlatformExtensionsDirectory = FPaths::ProjectPlatformExtensionsDir();
    UE_LOG(LogTemp, Warning, TEXT("FilePaths: ProjectPlatformExtensionsDir: %s"),*ProjectPlatformExtensionsDirectory);

자! 이렇게 로그가 출력이 되었네요.

LogTemp: Warning: FilePaths: ProjectDir: D:/Projects/UE4/UFS/
LogTemp: Warning: FilePaths: ProjectConfigDir: D:/Projects/UE4/UFS/Config/
LogTemp: Warning: FilePaths: ProjectContentDir: D:/Projects/UE4/UFS/Content/
LogTemp: Warning: FilePaths: ProjectIntermediateDirectory: D:/Projects/UE4/UFS/Intermediate/
LogTemp: Warning: FilePaths: ProjectLogDir: D:/Projects/UE4/UFS/Saved/Logs/
LogTemp: Warning: FilePaths: ProjectModsDir: D:/Projects/UE4/UFS/Mods/
LogTemp: Warning: FilePaths: ProjectPluginsDir: D:/Projects/UE4/UFS/Plugins/
LogTemp: Warning: FilePaths: ProjectSavedDir: D:/Projects/UE4/UFS/Saved/
LogTemp: Warning: FilePaths: ProjectUserDir: D:/Projects/UE4/UFS/
LogTemp: Warning: FilePaths: ProjectPersistentDownloadDir: D:/Projects/UE4/UFS/PersistentDownloadDir
LogTemp: Warning: FilePaths: ProjectPlatformExtensionsDir: D:/Projects/UE4/UFS/Platforms/

언리얼 엔진 4 에서 경로는 종종 FString 에 저장됩니다. 하지만 UFS(언리얼 파일 시스템)는 TCHAR*를 다루기 때문에 TString을 TCHAR*로 변환할 수 있어야 합니다. 변수 앞에 *를 추가하면 됩니다.

 

프로젝트 폴더 옆에 있습니다. 엔진 폴더를 가져올 수도 있습니다.

FString EngineDirectory = FPaths::EngineDir();
UE_LOG(LogTemp, Warning, TEXT("FilePaths: EngineDir: %s"),*EngineDirectory);

FString EngineConfigDirectory = FPaths::EngineConfigDir();
UE_LOG(LogTemp, Warning, TEXT("FilePaths: EngineConfigDir: %s"),*EngineConfigDirectory);

FString EngineContentDirectory = FPaths::EngineConfigDir();
UE_LOG(LogTemp, Warning, TEXT("FilePaths: EngineContentDir: %s"),*EngineContentDirectory);

FString EngineIntermediateDirectory = FPaths::EngineIntermediateDir();
UE_LOG(LogTemp, Warning, TEXT("FilePaths: EngineIntermediateDirectory: %s"),*EngineIntermediateDirectory);

FString EnginePluginsDirectory = FPaths::EnginePluginsDir();
UE_LOG(LogTemp, Warning, TEXT("FilePaths: EnginePluginsDir: %s"),*EnginePluginsDirectory);

FString EngineSavedDirectory = FPaths::EngineSavedDir();
UE_LOG(LogTemp, Warning, TEXT("FilePaths: EngineSavedDir: %s"),*EngineSavedDirectory);

FString EngineUserDirectory = FPaths::EngineUserDir();
UE_LOG(LogTemp, Warning, TEXT("FilePaths: EngineUserDir: %s"),*EngineUserDirectory);

FString EngineDefaultLayoutDir = FPaths::EngineDefaultLayoutDir();
UE_LOG(LogTemp, Warning, TEXT("FilePaths: EngineDefaultLayoutDir: %s"),*EngineDefaultLayoutDir);

FString EnginePlatformExtensionsDir = FPaths::EnginePlatformExtensionsDir();
UE_LOG(LogTemp, Warning, TEXT("FilePaths: EnginePlatformExtensionsDir: %s"),*EnginePlatformExtensionsDir);

FString EngineUserLayoutDir = FPaths::EngineUserLayoutDir();
UE_LOG(LogTemp, Warning, TEXT("FilePaths: EngineUserLayoutDir: %s"),*EngineUserLayoutDir);

이렇게~~

LogTemp: Warning: FilePaths: EngineDir: ../../../Engine/
LogTemp: Warning: FilePaths: EngineConfigDir: ../../../Engine/Config/
LogTemp: Warning: FilePaths: EngineContentDir: ../../../Engine/Config/
LogTemp: Warning: FilePaths: EngineIntermediateDirectory: ../../../Engine/Intermediate/
LogTemp: Warning: FilePaths: EnginePluginsDir: ../../../Engine/Plugins/
LogTemp: Warning: FilePaths: EngineSavedDir: C:/Users/Rene/AppData/Local/UnrealEngine/4.25/Saved/
LogTemp: Warning: FilePaths: EngineUserDir: C:/Users/Rene/AppData/Local/UnrealEngine/4.25/
LogTemp: Warning: FilePaths: EngineDefaultLayoutDir: ../../../Engine/Config/Layouts/
LogTemp: Warning: FilePaths: EnginePlatformExtensionsDir: ../../../Engine/Platforms/
LogTemp: Warning: FilePaths: EngineUserLayoutDir: C:/Users/Rene/AppData/Local/UnrealEngine/4.25/Saved/Config/Layouts/

이제 경로를 얻을 수 있는 방법이 생겼습니다. 이제 사용해 봅시다.

 

FPlatformFileManager, finding files and Directories

UFS(언리얼 파일 시스템)는 플랫폼에 구애받지 않습니다. 즉, 빌드하는 모든 플랫폼에서 작동합니다. 따라서 이걸 사용하기만 하면 PC, Mac, 오큘러스, 안드로이드 외 모든 플랫폼에서 작동합니다.
가장 먼저 해야 할 일은 FPlatformFileManager 를 사용하여 IPlatformFile& 을 생성하는 것입니다.

IPlatformFile& FileManager = FPlatformFileManager::Get().GetPlatformFile();

이제 FileManager를 사용하여 모든 종류의 작업을 수행할 수 있습니다.

 

 

 

FPlatformFileManager

Platform File chain manager.

docs.unrealengine.com

 

Does a directory Exist?

디렉터리를 사용하기 전에 안전을 위해 디렉터리가 존재하는지 확인하고 싶을 때가 많습니다. 이 함수를 사용하면 이 작업을 수행할 수 있습니다!

DirectoryExists(TCHAR*)

참 또는 거짓을 반환합니다. 다음은 디렉터리가 존재하는지 확인하는 함수 예제입니다!

 

// Getting the directory we want to check if it exists
FString MyConfigDirectory = FPaths::ProjectConfigDir();
MyConfigDirectory.Append(TEXT("MyConfigDir"));
// Creating the FileManager
IPlatformFile& FileManager = FPlatformFileManager::Get().GetPlatformFile();
/*This is where the magic happens.
* The DirectoryExist function takes in one argument a TCHAR*
* But since we have an FString we need to dereference it Using a *
*/
if(FileManager.DirectoryExists(*MyConfigDirectory))
{
    UE_LOG(LogTemp, Warning, TEXT("FilePaths: Directory Exists"));
}
else
{
    UE_LOG(LogTemp, Warning, TEXT("FilePaths: Directory Does not exist"));
}

이 경우 폴더가 존재하지 않으므로 다음과 같이 반환됩니다.

 

LogTemp: Warning: FilePaths: Directory Does not exist

Spawning and Destroying Actors in Blueprint

How do I create a directory?

이 경우 디렉터리가 없으므로 디렉터리를 만들어야 합니다. 함수를 사용하여 이 작업을 수행할 수 있습니다!

CreateDirectory(TChar*)

이 함수는 디렉터리가 생성되면 참을 반환하고 생성에 실패하면 거짓을 반환합니다. 다음은 디렉터리 생성 예제입니다.

 

// Getting the directory we want to check if it exists
FString MyConfigDirectory = FPaths::ProjectConfigDir();
MyConfigDirectory.Append(TEXT("MyConfigDir"));
// Creating the FileManager
IPlatformFile& FileManager = FPlatformFileManager::Get().GetPlatformFile();
/*This is where the magic happens.
* The CreateDirectory function takes in one argument a TCHAR*
* But since we have an FString we need to dereference it Using a *
*/
if(FileManager.CreateDirectory(*MyConfigDirectory))
{
    UE_LOG(LogTemp, Warning, TEXT("FilePaths: Directory was created"));
}
else
{
    UE_LOG(LogTemp, Warning, TEXT("FilePaths: Directory was not created"));
}

이 경우 디렉토리가 생성됩니다. 그리고 그 대가로 다음을 얻습니다.

 

LogTemp: Warning: FilePaths: Directory was created

How do I create a directory tree

때로는 하나의 폴더가 아니라 전체 폴더 트리를 만들고 싶을 때가 있습니다. 이 함수를 사용하면 이 작업을 수행할 수 있습니다!

CreateDirectoryTree(TChar*)

디렉터리가 생성되면 참을 반환하고 디렉터리 생성에 실패하면 거짓을 반환합니다. 다음은 디렉터리 트리를 만드는 예제입니다.

 

// Getting the directory we want to check if it exists
FString MyConfigDirectoryTree = FPaths::ProjectConfigDir();
MyConfigDirectoryTree.Append(TEXT("MyConfigDir/SecondFolder/ThirdFolder"));
// Creating the FileManager
IPlatformFile& FileManager = FPlatformFileManager::Get().GetPlatformFile();
/*This is where the magic happens.
* The CreateDirectoryTree function takes in one argument a TCHAR*
* But since we have an FString we need to dereference it Using a *
*/
if(FileManager.CreateDirectoryTree(*MyConfigDirectoryTree))
{
    UE_LOG(LogTemp, Warning, TEXT("FilePaths: DirectoryTree was created"));
}
else
{
    UE_LOG(LogTemp, Warning, TEXT("FilePaths: Directory Was not Created"));
}

이 경우 디렉터리 트리가 생성되어 그 대가로 이 파일을 얻습니다.

 

LogTemp: Warning: FilePaths: DirectoryTree was created

How do I Delete a directory?

이 경우 이미 디렉터리가 있지만 더 이상 원하지 않습니다. 함수를 사용하여 이 작업을 수행할 수 있습니다!

.DeleteDirectory(TChar*)

디렉터리가 삭제되면 참을 반환하고 디렉터리 삭제에 실패하면 거짓을 반환합니다. 다음은 디렉터리 삭제의 예입니다.

 

// Getting the directory we want to check if it exists
FString MyConfigDirectory = FPaths::ProjectConfigDir();
MyConfigDirectory.Append(TEXT("MyConfigDir"));
// Creating the FileManager
IPlatformFile& FileManager = FPlatformFileManager::Get().GetPlatformFile();
/*This is where the magic happens.
* The DeleteDirectory function takes in one argument a TCHAR*
* But since we have an FString we need to dereference it Using a *
*/
if(FileManager.DeleteDirectory(*MyConfigDirectory))
{
    UE_LOG(LogTemp, Warning, TEXT("FilePaths: Directory was Deleted"));
}
else
{
    UE_LOG(LogTemp, Warning, TEXT("FilePaths: Directory was not Deleted"));
}

이 경우 디렉터리가 삭제되었으므로 그 대가로 이 결과를 얻습니다.

 

LogTemp: Warning: FilePaths: Directory was Deleted

가끔 이 기능이 실패하는 경우가 있습니다. 그 이유는 폴더에 파일이 포함되어 있기 때문일 수 있습니다.

 

How do I Delete a directory with files in it?

디렉터리에 파일이 있는 경우 DeleteDirectory() 함수를 사용할 수 없습니다. 이 경우 함수를 사용해야 합니다!

.DeleteDirectoryRecursively(TCHAR*)

디렉터리와 디렉터리에 포함된 모든 파일 및 폴더가 삭제되면 참을 반환하고 디렉터리 삭제에 실패하면 거짓을 반환합니다. 다음은 디렉터리와 모든 하위 파일 및 폴더를 삭제하는 예제입니다.

 

// Getting the directory we want to check if it exists
FString MyConfigDirectory = FPaths::ProjectConfigDir();
MyConfigDirectory.Append(TEXT("MyConfigDir"));
// Creating the FileManager
IPlatformFile& FileManager = FPlatformFileManager::Get().GetPlatformFile();
/*This is where the magic happens.
* The DeleteDirectoryRecursively function takes in one argument a TCHAR*
* But since we have an FString we need to dereference it Using a *
*/
if(FileManager.DeleteDirectoryRecursively(*MyConfigDirectory))
{
    UE_LOG(LogTemp, Warning, TEXT("FilePaths: Directory and sub-files and folders are Deleted"));
}
else
{
    UE_LOG(LogTemp, Warning, TEXT("FilePaths: Directory and sub-files and folders are not Deleted"));
}

이 경우 디렉토리와 하위 파일 및 폴더가 모두 삭제되고 다음과 같은 반환이 표시됩니다.

 

 

LogTemp: Warning: FilePaths: Directory was Deleted

How do I find all files and folders in a directory

이것은 조금 더 복잡하고 추가 코딩이 필요합니다. 기본은 여전히 동일합니다. 파일 관리자에게 무언가를 요청하지만 약간의 추가 설정이 필요합니다. PlatformFile::FDirectoryVisitor 그리고 오버라이드에서 상속하는 구조체를 만들어야 합니다.

struct DirectoryVisitor : public IPlatformFile::FDirectoryVisitor
{
    //This function is called for every file or directory it finds.
    bool Visit(const TCHAR* FilenameOrDirectory, bool bIsDirectory) override
    {
        // did we find a Directory or a file?
        if(bIsDirectory)
        {
            UE_LOG(LogTemp, Warning, TEXT("FilePaths: Directory found: %s"),FilenameOrDirectory);
        }
        else
        {
            UE_LOG(LogTemp, Warning, TEXT("FilePaths: File Found: %s"),FilenameOrDirectory);
        }
        return true;
    }
};

이제 디렉토리 방문자 구조체를 만들었으므로 코드에서 사용할 수 있습니다.

 

FString MyConfigDirectoryTree = FPaths::ProjectConfigDir();
MyConfigDirectoryTree.Append(TEXT("MyConfigDir"));
IPlatformFile& FileManager = FPlatformFileManager::Get().GetPlatformFile();
//Now we need to create a DirectoryVisitor 
DirectoryVisitor Visitor;
// The ItterateDirectory takes two arguments The directory and the Visitor we created above.
if(FileManager.IterateDirectory(*MyConfigDirectoryTree,Visitor))
{
    UE_LOG(LogTemp, Warning, TEXT("FilePaths: Directory's or files found"));
}
else
{
    UE_LOG(LogTemp, Warning, TEXT("FilePaths: Directory did not exist or visitor returned false"));
}

이 경우, MyConfigDir에 테스트용으로 넣은 폴더와 파일이 많이 발견됩니다.

 

LogTemp: Warning: FilePaths: Directory found: D:/Projects/UE4/UFS/Config/MyConfigDir/ExampleFolder
LogTemp: Warning: FilePaths: Directory found: D:/Projects/UE4/UFS/Config/MyConfigDir/SecondFolder
LogTemp: Warning: FilePaths: File Found: D:/Projects/UE4/UFS/Config/MyConfigDir/TestFile1.txt
LogTemp: Warning: FilePaths: File Found: D:/Projects/UE4/UFS/Config/MyConfigDir/TestFile2.txt
LogTemp: Warning: FilePaths: Directory's or files found

How do I find all file's and folders in a directory Recursively

이 작업은 IterateDirectory 함수와 거의 동일합니다. IPlatformFile::FDirectoryVisitor 그리고 오버라이드에서 상속하는 Struct를 만들어야 합니다.

struct DirectoryVisitor : public IPlatformFile::FDirectoryVisitor
{
    //This function is called for every file or directory it finds.
    bool Visit(const TCHAR* FilenameOrDirectory, bool bIsDirectory) override
    {
        // did we find a Directory or a file?
        if(bIsDirectory)
        {
            UE_LOG(LogTemp, Warning, TEXT("FilePaths: Directory found: %s"),FilenameOrDirectory);
        }
        else
        {
            UE_LOG(LogTemp, Warning, TEXT("FilePaths: File Found: %s"),FilenameOrDirectory);
        }
        return true;
    }
};

이제 디렉토리 방문자 구조체를 만들었으므로 코드에서 사용할 수 있습니다.

 

FString MyConfigDirectoryTree = FPaths::ProjectConfigDir();
MyConfigDirectoryTree.Append(TEXT("MyConfigDir"));
IPlatformFile& FileManager = FPlatformFileManager::Get().GetPlatformFile();
//Now we need to create a DirectoryVisitor 
DirectoryVisitor Visitor;
// Here is the only difference We call IterateDirectoryRecursively instead of IterateDirectory
// The IterateDirectoryRecursively takes two arguments The directory and the Visitor we created above.
if(FileManager.IterateDirectoryRecursively(*MyConfigDirectoryTree,Visitor))
{
    UE_LOG(LogTemp, Warning, TEXT("FilePaths: Directory's or files found"));
}
else
{
    UE_LOG(LogTemp, Warning, TEXT("FilePaths: Directory did not exist or visitor returned false"));
}

이 경우, 테스트용으로 MyConfigDir에 넣은 폴더와 파일이 많이 발견되었습니다.

 

LogTemp: Warning: FilePaths: Directory found: D:/Projects/UE4/UFS/Config/MyConfigDir/ExampleFolder
LogTemp: Warning: FilePaths: Directory found: D:/Projects/UE4/UFS/Config/MyConfigDir/SecondFolder
LogTemp: Warning: FilePaths: Directory found: D:/Projects/UE4/UFS/Config/MyConfigDir/SecondFolder/ThirdFolder
LogTemp: Warning: FilePaths: File Found: D:/Projects/UE4/UFS/Config/MyConfigDir/SecondFolder/ThirdFolder/ThirdFolderFileExampleOne.txt
LogTemp: Warning: FilePaths: File Found: D:/Projects/UE4/UFS/Config/MyConfigDir/SecondFolder/ThirdFolder/ThirdFolderFileExampleTwo.txt
LogTemp: Warning: FilePaths: File Found: D:/Projects/UE4/UFS/Config/MyConfigDir/TestFile1.txt
LogTemp: Warning: FilePaths: File Found: D:/Projects/UE4/UFS/Config/MyConfigDir/TestFile2.txt
LogTemp: Warning: FilePaths: Directory's or files found

보시다시피, 지정된 디렉토리 내의 폴더도 확인했기 때문에 더 많은 폴더와 파일을 찾을 수 있습니다.

 

FPlatformFileManager Working with Files and Directories

디렉토리와 파일을 얻을 수 있다는 것은 좋은 일입니다. 하지만 그것으로 무언가를 해야 합니다. 다음은 복사, 삭제, 이동 등과 같은 몇 가지 유용한 기본 기능입니다...

Check if a file exist.

파일을 조작하기 전에 파일이 존재하는지 확인해야 합니다. 파일이 존재하지 않으면 몇 가지 심각한 오류가 발생합니다. 파일이 존재하는지 확인하기 위해 간단한 기능을 사용합니다.

.FileExist(TCHAR*);

다음은 그 예입니다.

 

FString MyFilePath = FPaths::ProjectConfigDir();
MyFilePath.Append(TEXT("MyConfigDir/FakeConfig.txt"));
IPlatformFile& FileManager = FPlatformFileManager::Get().GetPlatformFile();

if(FileManager.FileExists(*MyFilePath))
{
    UE_LOG(LogTemp, Warning, TEXT("FilePaths: File found!"));
}
else
{
    UE_LOG(LogTemp, Warning, TEXT("FilePaths: File not found!"));
}

파일이 존재하고 로그에 이 내용이 있습니다.

 

LogTemp: Warning: FilePaths: File found!

LogTemp: 경고: 파일 경로: 파일을 찾았습니다!

Copy A file.

파일을 복사하면 원본은 그대로 유지됩니다. 이를 원하지 않는다면 이동 기능을 살펴보세요.
이 기능을 사용하여 파일을 복사할 수 있습니다:

.CopyFile(TCHAR * Destination,TCHAR * source, EPlatformFileRead ReadFlags, EPlatformFileWrite WriteFlags)

다음은 그 예입니다.

 

FString MyFileSource = FPaths::ProjectConfigDir();
MyFileSource.Append(TEXT("MyConfigDir/FakeConfig.txt"));

FString MyFileDestination = FPaths::ProjectConfigDir();
MyFileDestination.Append(TEXT("MyConfigDir/SecondFolder/FakeConfig.txt"));

IPlatformFile& FileManager = FPlatformFileManager::Get().GetPlatformFile();

UE_LOG(LogTemp, Warning, TEXT("FilePaths: Dest: %s"), *MyFileDestination);
UE_LOG(LogTemp, Warning, TEXT("FilePaths: Source: %s"), *MyFileSource);

if(FileManager.CopyFile(*MyFileDestination,*MyFileSource,EPlatformFileRead::None,EPlatformFileWrite::None))
{
   UE_LOG(LogTemp, Warning, TEXT("FilePaths: File Copied!"));
}
else
{
   UE_LOG(LogTemp, Warning, TEXT("FilePaths: File not Copied!"));
}

파일이 존재하고 로그에 이 내용이 있습니다.

 

LogTemp: Warning: FilePaths: File Copied!

MoveFile

파일 이동은 파일 복사와 매우 유사하지만 원본을 제거합니다. 또한 조금 더 간단합니다.
이 기능을 사용하여 파일을 이동할 수 있습니다:

.MoveFile(TCHAR* Destination,TCHAR* SOURCE);

다음은 그 예입니다.

 

FString MyFileSource = FPaths::ProjectConfigDir();
MyFileSource.Append(TEXT("MyConfigDir/FakeConfig.txt"));

FString MyFileDestination = FPaths::ProjectConfigDir();
MyFileDestination.Append(TEXT("MyConfigDir/SecondFolder/FakeConfig.txt"));

IPlatformFile& FileManager = FPlatformFileManager::Get().GetPlatformFile();

if(FileManager.MoveFile(*MyFileDestination,*MyFileSource))
{
   UE_LOG(LogTemp, Warning, TEXT("FilePaths: File Moved!"));
}
else
{
   UE_LOG(LogTemp, Warning, TEXT("FilePaths: File not Moved!"));
}

파일이 존재하고 로그에 이 내용이 있습니다.

 

LogTemp: Warning: FilePaths: File Moved!

Delete a file

파일을 삭제하는 방법은 매우 간단합니다.

.DeleteFile(TCHAR*))

다음은 그 예입니다.

 

FString FileToDelete = FPaths::ProjectConfigDir();
FileToDelete.Append(TEXT("MyConfigDir/FakeConfig.txt"));

IPlatformFile& FileManager = FPlatformFileManager::Get().GetPlatformFile();

if(FileManager.DeleteFile(*FileToDelete))
{
   UE_LOG(LogTemp, Warning, TEXT("FilePaths: File Deleted!"));
}
else
{
   UE_LOG(LogTemp, Warning, TEXT("FilePaths: File not Deleted!"));
}

파일이 존재하고 로그에 이 내용이 있습니다.

 

LogTemp: Warning: FilePaths: File Deleted!

File Size

파일 크기를 가져오는 것은 다른 함수와 조금 더 다릅니다. 이 함수는 부울 대신 파일 크기가 바이트 단위로 표시된 Int를 반환합니다.

파일 크기를 얻으려면 이 함수를 사용합니다:

.FileSize(TCHAR*);

다음은 그 예입니다.

 

FString File = FPaths::ProjectConfigDir();
File.Append(TEXT("MyConfigDir/FakeConfig.txt"));

IPlatformFile& FileManager = FPlatformFileManager::Get().GetPlatformFile();
// Getting the file size and storring it
int FileSize = FileManager.FileSize(*File);
//The file size is -1 if it is not found
if(FileSize > 0)
{
   UE_LOG(LogTemp, Warning, TEXT("FilePaths: File Size: %d"),FileSize);
}
else
{
   UE_LOG(LogTemp, Warning, TEXT("FilePaths: File not found!"));
}

파일이 존재하고 로그에 이 내용이 있습니다.

 

LogTemp: Warning: FilePaths: File Size: 21