ファイル一覧を取得して配列に格納するブループリントの作成 #UE4 #CPP 

# UE4 CPP ファイル一覧を取得して配列に格納する

## C++クラスの追加
![image](https://qiita-image-store.s3.amazonaws.com/0/111261/a3fd5e90-7a25-4106-47da-e7c6eb17a745.png)

![image](https://qiita-image-store.s3.amazonaws.com/0/111261/69327402-d524-a184-7a0e-6df1ccb3db72.png)

![image](https://qiita-image-store.s3.amazonaws.com/0/111261/906ab01b-ec43-9b97-5a10-b38c46ab1a28.png)


## Blueprintで呼べるHelloWorld関数を作成する

```cpp:DirectoryInfo.h

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "Kismet/BlueprintFunctionLibrary.h"
#include "DirectoryInfo.generated.h"

/**
 * 
 */
UCLASS()
class CPPSAMPLE_API UDirectoryInfo : public UBlueprintFunctionLibrary
{
	GENERATED_BODY()
public :
	UFUNCTION(BlueprintCallable, Category = "MyBPLibrary")
	static FString HelloWorld();
	
};

```

```cpp:DirectoryInfo.cpp

// Fill out your copyright notice in the Description page of Project Settings.

#include "CPPSample.h"
#include "DirectoryInfo.h"

FString UDirectoryInfo::HelloWorld()
{
	return FString("Hello World!!");
}


```

![image](https://qiita-image-store.s3.amazonaws.com/0/111261/558f65d6-7f3b-1d77-ef38-f612652603da.png)

## フルパスを指定して、ファイル名の配列を返す関数を作成する


```cpp:DirectoryInfo.h

// Fill out your copyright notice in the Description page of Project Settings.

#pragma once

#include "Kismet/BlueprintFunctionLibrary.h"
#include "DirectoryInfo.generated.h"

/**
 * 
 */
UCLASS()
class CPPSAMPLE_API UDirectoryInfo : public UBlueprintFunctionLibrary
{
	GENERATED_BODY()
public :
	UFUNCTION(BlueprintCallable, Category = "MyBPLibrary")
	static FString HelloWorld();
	
	UFUNCTION(BlueprintCallable, Category = "MyBPLibrary")
	static TArray<FString> GetAllFilesInDirectory(const FString directory, const bool fullPath, const bool withoutExtention, const FString onlyFilesStartingWith, const FString onlyFilesWithExtension);

};


```

```cpp:DirectoryInfo.cpp

// Fill out your copyright notice in the Description page of Project Settings.


#include "CPPSample.h"
#include "DirectoryInfo.h"

FString UDirectoryInfo::HelloWorld()
{
	return FString("Hello World!!");
}

TArray<FString> UDirectoryInfo::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;
}

```


## 参照

https://docs.unrealengine.com/latest/JPN/Programming/UnrealArchitecture/TArrays/index.html

https://answers.unrealengine.com/questions/212791/how-to-get-file-list-in-a-directory.html

http://stackoverflow.com/questions/29800808/how-to-get-filename-without-extension-in-unreal