Implemented function to calculate directory/filesizes

This commit is contained in:
Leon Etienne (ubuntu wsl) 2020-09-28 18:02:07 +02:00
parent 18cdc94183
commit 336881db75
3 changed files with 50 additions and 3 deletions

View File

@ -1,4 +1,7 @@
#include "Filesystem.h"
#ifdef _WIN
#include <Windows.h>
#endif
std::string FileSystem::ReadFile(std::string filename)
{
@ -42,14 +45,14 @@ bool FileSystem::ExistsDirectory(std::string name)
return (!Exists(name) && (std::filesystem::exists(name)));
}
bool FileSystem::CreateDirectory(std::string name)
bool FileSystem::_CreateDirectory(std::string name)
{
return std::filesystem::create_directories(name);
}
bool FileSystem::CreateDirectoryIfNotExists(std::string name)
{
if (!ExistsDirectory(name)) return CreateDirectory(name);
if (!ExistsDirectory(name)) return _CreateDirectory(name);
return false;
}
@ -81,3 +84,43 @@ bool FileSystem::Delete(std::string filename)
remove(filename.c_str());
return true;
}
#include <iostream>
long long int FileSystem::CalculateSize(std::string name)
{
#ifdef _WIN
WIN32_FIND_DATAA data;
HANDLE sh = NULL;
long long int byteCount = 0;
//std::cout << "Scanning dir " << name << std::endl;
sh = FindFirstFileA((name + "\\*").c_str(), &data);
if (sh == INVALID_HANDLE_VALUE) return -1;
do
{
if ((std::string(data.cFileName).compare(".") != 0) && (std::string(data.cFileName).compare("..") != 0))
{
// If found file-object is...
if ((data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY)
{
// ... a directory, search it recursively
byteCount += CalculateSize(name + "\\" + data.cFileName);
}
else
{
// ... a file, get its size
byteCount += (long long int)((data.nFileSizeHigh * MAXDWORD) + data.nFileSizeLow);
}
}
} while (FindNextFileA(sh, &data));
FindClose(sh);
return byteCount;
#else
return -1;
#endif
}

View File

@ -15,9 +15,10 @@ public:
static bool Copy(std::string from, std::string to);
static bool Delete(std::string filename);
static bool ExistsDirectory(std::string name);
static bool CreateDirectory(std::string name);
static bool _CreateDirectory(std::string name);
static bool CreateDirectoryIfNotExists(std::string name);
static bool DeleteDirectory(std::string name);
static long long int CalculateSize(std::string name);
private:
};

View File

@ -2,6 +2,9 @@
int main()
{
std::cout << (FileSystem::CalculateSize("C:\\Users\\Leon\\source\\repos\\Tubio\\Tubio") / 1024 / 1024) << std::endl;
return 0;
Framework framework;
framework.Run();