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;
}
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).
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/
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.
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.
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();
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With