[chronojump] Method for getting parentDir size with subdirs (or any dir)



commit 15d9a0ea6fe3a553932efeca1145b8837272783d
Author: Xavier de Blas <xaviblas gmail com>
Date:   Thu Aug 19 18:50:03 2021 +0200

    Method for getting parentDir size with subdirs (or any dir)

 src/util.cs | 35 +++++++++++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)
---
diff --git a/src/util.cs b/src/util.cs
index e1cac146c..a4afcc36e 100644
--- a/src/util.cs
+++ b/src/util.cs
@@ -1396,6 +1396,41 @@ public class Util
                sizeInKB = (int) UtilAll.DivideSafe(totalSize, 1024);
        }
 
+       //TODO: maybe this will need a thread
+       public static int GetFullDataSize (bool includingOldBackupsDir)
+       {
+               long fullDataSize = DirSizeWithSubdirs (new DirectoryInfo(GetParentDir(false)));
+               int sizeInKB = (int) UtilAll.DivideSafe(fullDataSize, 1024);
+
+               if(! includingOldBackupsDir)
+               {
+                       int filesBackup, sizeInKBBackup;
+                       Util.GetBackupsSize (out filesBackup, out sizeInKBBackup);
+                       sizeInKB -= sizeInKBBackup;
+               }
+
+               return sizeInKB;
+       }
+
+       //https://stackoverflow.com/a/468131
+       public static long DirSizeWithSubdirs (DirectoryInfo d)
+       {
+               long size = 0;
+               // Add file sizes.
+               FileInfo[] fis = d.GetFiles();
+               foreach (FileInfo fi in fis)
+               {
+                       size += fi.Length;
+               }
+               // Add subdirectory sizes.
+               DirectoryInfo[] dis = d.GetDirectories();
+               foreach (DirectoryInfo di in dis)
+               {
+                       size += DirSizeWithSubdirs (di);
+               }
+               return size;
+       }
+
        public static bool FileDelete(string fileName) 
        {
                LogB.Information("Checking if this filename exists: " + fileName);


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]