Home > OS >  ASP.NET: Fast way to get file size and count of all files?
ASP.NET: Fast way to get file size and count of all files?

Time:03-15

What modification to the code below, from:

https://www.devcurry.com/2010/07/calculate-size-of-folderdirectory-using.html

would need to be made to also return the count of all files in a folder and sub folders?

And could it be done at the same time in order to save time?

C#

using System;
using System.Linq;
using System.IO;

namespace ConsoleApplication3
{
  class Program
  {
    static void Main(string[] args)
    {
      DirectoryInfo dInfo = new DirectoryInfo(@"C:/Articles");
      // set bool parameter to false if you
      // do not want to include subdirectories.
      long sizeOfDir = DirectorySize(dInfo, true);

      Console.WriteLine("Directory size in Bytes : "  
      "{0:N0} Bytes", sizeOfDir);
      Console.WriteLine("Directory size in KB : "  
      "{0:N2} KB", ((double)sizeOfDir) / 1024);
      Console.WriteLine("Directory size in MB : "   
      "{0:N2} MB", ((double)sizeOfDir) / (1024 * 1024));

      Console.ReadLine();
    }

    static long DirectorySize(DirectoryInfo dInfo, bool includeSubDir)
    {
       // Enumerate all the files
       long totalSize = dInfo.EnumerateFiles()
                    .Sum(file => file.Length);

       // If Subdirectories are to be included
       if (includeSubDir)
       {
          // Enumerate all sub-directories
          totalSize  = dInfo.EnumerateDirectories()
                   .Sum(dir => DirectorySize(dir, true));
       }
       return totalSize;
    }
  }
}

VB

Imports System
Imports System.Linq
Imports System.IO

Module Module1

Sub Main()
   Dim dInfo As New DirectoryInfo("C:/Articles")
   ' set bool parameter to false if you
   ' do not want to include subdirectories.
   Dim sizeOfDir As Long = DirectorySize(dInfo, True)

   Console.WriteLine("Directory size in Bytes : " & _
    "{0:N0} Bytes", sizeOfDir)
   Console.WriteLine("Directory size in KB : " & _
    "{0:N2} KB", (CDbl(sizeOfDir)) / 1024)
   Console.WriteLine("Directory size in MB : " & _
    "{0:N2} MB", (CDbl(sizeOfDir)) / (1024 * 1024))

   Console.ReadLine()
End Sub

Private Function DirectorySize(ByVal dInfo As DirectoryInfo, _
   ByVal includeSubDir As Boolean) As Long
   ' Enumerate all the files
   Dim totalSize As Long = dInfo.EnumerateFiles() _
     .Sum(Function(file) file.Length)

   ' If Subdirectories are to be included
   If includeSubDir Then
     ' Enumerate all sub-directories
     totalSize  = dInfo.EnumerateDirectories() _
      .Sum(Function(dir) DirectorySize(dir, True))
   End If
   Return totalSize
End Function

End Module

I assume also that it could be executed in a thread and the results stored in a database in case it takes a long time.

UPDATE 1:

From File count from a folder I see an answer (though not the accepted one) to get a file count uses LINQ like the code above:

var fileCount = (from file in Directory.EnumerateFiles(@"H:\iPod_Control\Music", "*.mp3", SearchOption.AllDirectories)
                        select file).Count();

I haven't a huge amount of experience with LINQ and so was wondering if the code should or could be combined so the LINQ returns two values (size and count) to save time? I could have a separate thread for each task but thought it could be more efficient if there only needed to be one pass per folder.

We would want it to be able to handle millions of sub folders with a .png and a .jpg file in each.

Our folders look like:

 /images/3/4/2/4/7/6/5/4/3/4/3/2/abc-234345674243.png

CodePudding user response:

Try dumping recursion, and try dumping linq - it is slow and eats up a lot of memory.

Try this:

  Dim strFolder = "C:\Users\AlbertKallal\Desktop"

    Dim MyDir As New DirectoryInfo(strFolder)
    Dim MyFiles() As FileInfo = MyDir.GetFiles("*.*", SearchOption.AllDirectories)



    For Each MyFile As FileInfo In MyFiles
        tSize  = MyFile.Length
    Next
  • Related