techbro
asked on
Finding the largest file in the directory
I am studying for MCTS exam, and coding the largest file in the directory in the practice test.
The problem I am facing is in Line 43, which contains the maximum file size. This code is giving me all name and file sizes, but I only need to largest one. If I place the line 43 after the loop, then variables will be out of scope/unreachable. What changes do I need to make to get only one largest file for each folder?
The problem I am facing is in Line 43, which contains the maximum file size. This code is giving me all name and file sizes, but I only need to largest one. If I place the line 43 after the loop, then variables will be out of scope/unreachable. What changes do I need to make to get only one largest file for each folder?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace DrivesFolders
{
//Populate with the local computer's drives, folders, and files
class Program
{
static void Main(string[] args)
{
// foreach (DriveInfo di in DriveInfo.GetDrives())
{
// Console.WriteLine(" {0} ({1})", di.Name, di.DriveType);
// DirectoryInfo dir = new DirectoryInfo(@di.Name);
DirectoryInfo dir = new DirectoryInfo(@"C:\");
foreach (DirectoryInfo dirinfo in dir.GetDirectories())
{
Console.WriteLine("\n\nFolder");
Console.WriteLine("--------------");
Console.WriteLine(dirinfo.Name);
Console.WriteLine("\nFiles");
Console.WriteLine("-----");
foreach (FileInfo fi in dirinfo.GetFiles())
{
long max=0;
// long min =0;
string maxLength ="";
if (fi.Length > max)
{
maxLength = fi.Name;
max = fi.Length;
}
else
break;
Console.WriteLine("{0} ({1})", maxLength, max);
}
}
}
Console.ReadKey();
}
}
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Right...declare them outside the loop; line #23 would work great. =)
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
kaufmed,
You are right. May be the I need to remove the "break" statement after "else".
I actually thought "foreach" statement is checking each and every file.
Here is what I thought in sequence:
1. File1 is greater than "max" (size = 0), then variables are initialized "maxLength = File1" and "max =100" variables.
2. File2 (size 50) is less than "max" (size = 100), Nothing Happens
3. File3 (size 200) is greater than "max" (size 100), then variables are initialized "maxLength = File3" and "max = 200".
Output: File3 200 (maximum file size)
Let me know if I got this right or missing something. Thank you for noticing.
You are right. May be the I need to remove the "break" statement after "else".
I actually thought "foreach" statement is checking each and every file.
Here is what I thought in sequence:
1. File1 is greater than "max" (size = 0), then variables are initialized "maxLength = File1" and "max =100" variables.
2. File2 (size 50) is less than "max" (size = 100), Nothing Happens
3. File3 (size 200) is greater than "max" (size 100), then variables are initialized "maxLength = File3" and "max = 200".
Output: File3 200 (maximum file size)
Let me know if I got this right or missing something. Thank you for noticing.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I actually thought "foreach" statement is checking each and every file.Yes, you can't know which is the largest file without checking every file, unless you had some function that returned the files in an order sorted by size. I don't believe the Framework has such a built-in function, though.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thank you for your response,
I have received multiple solutions for the problem I have, so I am distributing points to all participants.
I have received multiple solutions for the problem I have, so I am distributing points to all participants.