Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Recursion the Best Option for Determining the Max File Size in a Directory

Tags:

c#

I wrote the following method to determine the max file size:

    public static long GetMaxFileSize(string dirPath, long maxFileSize)
    {
        DirectoryInfo [] dirInfos = new DirectoryInfo(dirPath).GetDirectories();
        foreach (DirectoryInfo dirInfo in dirInfos)
        {
            DirectoryInfo [] subDirInfos = dirInfo.GetDirectories();
            foreach (DirectoryInfo subDirInfo in subDirInfos)
                maxFileSize = GetMaxFileSize(dirInfo.FullName, maxFileSize);

            FileInfo [] fileInfos = dirInfo.GetFiles();
            foreach (FileInfo fileInfo in fileInfos)
            {
                if (maxFileSize < fileInfo.Length)
                    maxFileSize = fileInfo.Length;
            }
        }

        return maxFileSize;
    }

Code Complete recommends to "use recursion selectively". That being the case, I was wondering if the community thought this was a valid use of recursion. If not, is there a better technique of doing this?

EDIT: I can't use LINQ because its not available in .NET 2.0, but I don't want to tag this as a .NET 2.0 question only to further discussion points like Jared's below.

EDIT: Cleaned up code based on an issue that was spotted in not getting the root directory's files.

   public static long GetMaxFileSize(DirectoryInfo dirInfo, long maxFileSize)
   {
       DirectoryInfo [] subDirInfos = dirInfo.GetDirectories();
       foreach (DirectoryInfo subDirInfo in subDirInfos)
       {
           maxFileSize = GetMaxFileSize(subDirInfo, maxFileSize);
       }

       FileInfo [] fileInfos = dirInfo.GetFiles();
       foreach (FileInfo fileInfo in fileInfos)
       {
           if (maxFileSize < fileInfo.Length)
               maxFileSize = fileInfo.Length;
       }

       return maxFileSize;
   }
like image 630
Blake Blackwell Avatar asked Jan 14 '10 16:01

Blake Blackwell


People also ask

Which of the following options can be used to know the size of file?

Right-click the file and click Properties. The image below shows that you can determine the size of the file or files you have highlighted from in the file properties window. In this example, the chrome. jpg file is 18.5 KB (19,032 bytes), and that the size on disk is 20.0 KB (20,480 bytes).

How do you list a recursive file size and directories in a directory?

Type du -a /dir/ | sort -n -r | head -n 20. du will estimate file space usage. sort will sort out the output of du command. head will only show top 20 largest file in /dir/

How do you find the maximum in an array using recursion?

Take array Arr[] as input. Function recforMax(int arr[], int len) takes input array and its length and returns maximum in the array using recursion. Take the integer variable maximum. If the current index len is 1 then set maximum=arr[0] and return maximum.

What is a recursive directory search?

If you are searching recursively, it means that you search the current directory /home/user1 , and any subdirectories (like /home/user1/documents ), and any subdirectories of subdirectories (like /home/user1/documents/tests ), etc.


2 Answers

I think a better way is to the File System API to do the searching for you via Directory.GetFiles. This method provides automatic searching of sub-directories. This eliminates the question of whether or not to recurse and instead leaves the decision of how to implement it on the designer of the API (who likely designed it for such a scenario).

This method combined with LINQ provides a very elegant solution

var max = Directory
  .GetFiles(path, "*", SearchOption.AllDirectories)
  .Select(x => new FileInfo(x))
  .Select(x => x.Length)
  .Max();

EDIT As Jimmy pointed out, for 4.0 and higher, it's better to use EnumerateFiles to avoid the overhead of creating a potentially large array

var max = Directory
  .EnumerateFiles(path, "*", SearchOption.AllDirectories)
  .Select(x => new FileInfo(x))
  .Select(x => x.Length)
  .Max();
like image 128
JaredPar Avatar answered Oct 10 '22 22:10

JaredPar


As far as tree traversal goes, I think recursion is a fantastic fit. (The directory structure is a tree)

As long as you know that the directory structure isn't unbelievably huge, you shouldn't have to worry about overflowing the stack.

Recursive solutions to navigate trees are almost always more elegant than iterative solutions

like image 40
jjnguy Avatar answered Oct 10 '22 22:10

jjnguy