From 336881db75411c0bdb06c0353f3e6ced226726d7 Mon Sep 17 00:00:00 2001 From: "Leon Etienne (ubuntu wsl)" Date: Mon, 28 Sep 2020 18:02:07 +0200 Subject: [PATCH] Implemented function to calculate directory/filesizes --- Tubio/FileSystem.cpp | 47 ++++++++++++++++++++++++++++++++++++++++++-- Tubio/FileSystem.h | 3 ++- Tubio/main.cpp | 3 +++ 3 files changed, 50 insertions(+), 3 deletions(-) diff --git a/Tubio/FileSystem.cpp b/Tubio/FileSystem.cpp index 39149a9..ed45724 100644 --- a/Tubio/FileSystem.cpp +++ b/Tubio/FileSystem.cpp @@ -1,4 +1,7 @@ #include "Filesystem.h" +#ifdef _WIN +#include +#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 +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 +} diff --git a/Tubio/FileSystem.h b/Tubio/FileSystem.h index b34365f..761c1c8 100644 --- a/Tubio/FileSystem.h +++ b/Tubio/FileSystem.h @@ -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: }; diff --git a/Tubio/main.cpp b/Tubio/main.cpp index c203008..4b6817e 100644 --- a/Tubio/main.cpp +++ b/Tubio/main.cpp @@ -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();