C++ File operations not working as expected

Hey there folks

I have an issue with some C++ code im using to write a level loading routine.

Seems everything works fine, but the code finds 0 files even with a blank extension (which should return all files regardless of extension).

Can anyone see my mistake? Code is a little crude at the moment as ive just started and its basic debugging code only right now.

TArray ULevelLoader::GetLevels(FString Folder, FString Extension) {
TArray Levels;
IPlatformFile& PlatformFile = FPlatformFileManager::Get().GetPlatformFile();
FString Directory = GetContentDirectory() + Folder;

if (!PlatformFile.DirectoryExists(*Directory))
{
	UE_LOG(LogTemp,Warning, TEXT("Directory not found - %s") , *Directory);
	return Levels;
}
else {
	UE_LOG(LogTemp, Warning, TEXT("Folder Exists! - %s"), *Directory);
	PlatformFile.FindFiles(Levels, *Folder,*Extension);

	int Count = Levels.Num();
	FString txt = FString::FromInt(Count);
	UE_LOG(LogTemp, Warning, TEXT("Found %s Files"), *txt);

	for (int x = 0; x < Count; x++) {
		UE_LOG(LogTemp, Warning, TEXT("Found - %s"), *Levels[x]);
	}
	return Levels;
}

}

Not sure why the code block isnt formatting properly sorry

OK after experimenting with absolute file paths i have found that the GetContentDirectory() isnt working with find files. But it does work with DirectoryExists()? Go figure.

Solution is using absolute filepaths, but that isnt very helpful as once the game is installed the user could have chose a different location than the C:\ drive.

So how best to get the current running games’ directory paths? Beyond manually storing it somewhere.

To alleviate the absolute path problem you can use:

FString contentPath = FPaths::ProjectContentDir();
FString fullContentPath = FPaths::ConvertRelativePathToFull(contentPath);

then just stick on the file name

FPaths requires
#include “Misc/Paths.h”

1 Like

So many ways to do the same thing when you search this stuff on google!

Thank you, thats exactly what i needed. Just a thought tho. I am using FPaths in another part of my code WITHOUT the #include, does 5.1 not need it anymore? go figure. but thanks again!

I just go by the documentation and include it just in case. It could be part of the core so if you omit the include and it runs then it should be ok.

It seems there is a problem when i use this , if i manually enter a full path into my into my blueprint it works, if i take the resultant FString from this code and use it , it fails. Very strange as the debug text i output to the log is identical to the hand typed path. Any idea what could be causing this? is there some eroneous hidden characters im missing?

directory exists, that part works from the resultant absolute path, but it findfiles doesnt like it. but i type the exact string path into the variable in blueprint it works. the strings are identical im sure of it, i cannot see any difference. and it works in the directoryexists section.

Am i crazy?

If i remember unreal uses / for paths check if you have any \ in your search paths.
I’m away from my pc atm but I’ll try to recreate your function later on.

My first port of call was that and indeed they are different, but i will now try replacing them to windows style absolute as if i typed it, tho the directoryexists code can use either or and even both :face_with_monocle:

thanks again i will reply if it makes a difference , or not too :rofl:

Well all of a sudden works again, didnt change anything that suddenly caused it, maybe its my laptop who knows. Your code is perfectly fine @3dRaven

Thanks again

1 Like

Glad you got it running. Perhaps it just needed a recompile or restart :wink:

Yeah well it seems every time I modify a C++ file I have to completely reload the project, this sucks for debugging but oh well. it works now!