FileInfo filter by size
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: FileInfo filter by size

  1. #1
    Join Date
    Dec 2012
    Posts
    3

    FileInfo filter by size

    Hey, I'm a fairly new programmer and currently working on a University assignment and theres an extra feature I want to implement but can't seem to work it out

    I have these two lines at the start of my program

    DirectoryInfo folderInfo = new DirectoryInfo(C:\\Windows);
    FileInfo[] files = folderInfo.GetFiles();

    but I've been trying to create a new FileInfo array that will only contain files from DirectoryInfo if the file size is within a certain range of a user inputted value

    Someone recommended I use a where command which I haven't been taught yet, and searching online hasn't helped much and I just ended up with a line like

    FileInfo[] sizeFiltered = fileArray.Where(f => f.Length >= lowerBound && f.Length <= upperBound)

    which made my compiler angry.
    Any help on fixing my problem would be greatly appreciated

  2. #2
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,000

    Re: FileInfo filter by size

    I am not a fan of Linq. Instead just loop:

    Code:
    List<FileInfo> sizeFiltered = new List<FileInfo>();
    foreach(FileInfo f in files)
    {
        if( f.Length >= lowerBound && f.Length >= upperBound )
            sizeFiltered.Append(f);
    }
    Probably though your compiler is angry because Where is returning an IEnumerable<FileInfo>, not a FileInfo[] (??). You can probably call .ToArray() (or maybe .ToArray<FileInfo>() -- not sure) and get your Linq query to work. Elsewise, post the error message.
    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  3. #3
    Join Date
    Dec 2012
    Posts
    3

    Re: FileInfo filter by size

    Okay my compiler isn't angry any more but I'm having a bug when trying to display the contents of the list
    I'm trying to receive an input number from the user and then displaying information about files that are within 1 mb of that chosen size. My code is below and it runs, but when I try entering a value of 1, I receive some results where the size is greater than 1 mb difference. if anyone can spot the reason for this in my code it would be greatly appreciated

    Code:
                int mb = 1048576;
                int userSize = 0;
                bool sizeIsNumber = int.TryParse(Console.ReadLine(), out userSize);
    
                while (sizeIsNumber == false || userSize < 0)
                {
                    Console.Clear();
                    Console.WriteLine("That is an invalid selection, please enter a whole number greater than 0");
                    sizeIsNumber = int.TryParse(Console.ReadLine(), out userSize);
                }
                
                int lowerBound = userSize - mb;
                int upperBound = userSize + mb;
    
                if (lowerBound < 0)
                {
                    lowerBound = 0;
                }
    
                List<FileInfo> sizeFiltered = new List<FileInfo>();
    
                foreach (FileInfo f in fileArray)
                {
                    if (f.Length >= lowerBound && f.Length <= upperBound)
                    {
                        sizeFiltered.Add(f);
                    }
                }
    
                for (int i = 0; i < sizeFiltered.Count; i++)
                {
                    Console.Write("{0}. ", i + 1);                 
                    Console.Write(fileArray[i].Name);              
                    Console.Write(" ({0})", fileArray[i].Length);   
                    Console.WriteLine();                          
                }
    Last edited by BioPhysEngr; December 22nd, 2012 at 11:31 AM. Reason: added code and /code tags

  4. #4
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,000

    Re: FileInfo filter by size

    Code:
    for (int i = 0; i < sizeFiltered.Count; i++)
    {
        Console.Write("{0}. ", i + 1);                 
        Console.Write(sizeFiltered[i].Name);              
        Console.Write(" ({0})", sizeFiltered[i].Length);   
        Console.WriteLine();                          
    }
    N.B., you can do this in one statement instead of four:
    Code:
    Console.WriteLine("{0}. {1} {2}", (i+1), sizeFiltered[i].Name, sizeFiltered[i].Length);
    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  5. #5
    Join Date
    Dec 2012
    Posts
    3

    Re: FileInfo filter by size

    oh wow I feel dumb haha. Thanks very much

  6. #6
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,000

    Re: FileInfo filter by size

    An easy mistake to make! As I learned when programming in assembly (for which there was no debugger I liked): code review - just re-reading through your own code - is the single most powerful debugging tool you have at your disposal!
    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center