Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5951669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:34:14+00:00 2026-05-22T17:34:14+00:00

How can I simplify this? I am trying to get the count of Excel

  • 0

How can I simplify this? I am trying to get the count of Excel files from a directory and subdirectories based on their size. I have at least 10 different groupings.

var queryList2Only = from i in di.GetFiles("*.xls", SearchOption.TopDirectoryOnly)
                                 .Where(f => f.Length <= 5120)
                     select i.Length;
if (queryList2Only.Any())
{
    dest.WriteLine("Excel File <= 5 KB");
    dest.WriteLine(queryList2Only.Count());
    dest.WriteLine("");
}

var queryList3Only = from i in di.GetFiles("*.xls", SearchOption.TopDirectoryOnly)
                                 .Where(f => f.Length > 5120 && f.Length <= 10240)
                     select i.Length;
if (queryList3Only.Any())
{
    dest.WriteLine("Excel File > 5 KB and <= 10 KB");
    dest.WriteLine(queryList3Only.Count());
    dest.WriteLine("");

EDIT:
I need this

  <= 5 KB,> 5 KB and <= 10 KB,> 10 KB and <= 20 KB,> 20 KB and <= 100 KB,> 100 KB and <= 1000 KB,> 1000 KB and <=5 MB,> 5 MB and <=10 MB,> 10 MB and <=20 MB,> 20 MB and <=50 MB,> 50 MB and <=100 MB

private void button1_Click(object sender, EventArgs e)
        {



            DirectoryInfo Folder = new DirectoryInfo(textBox1.Text);
            var _logFolderPath4 = Path.Combine(textBox1.Text.Trim(), "log");
            if (Folder.Exists)

                if (!Directory.Exists(_logFolderPath4))
                    Directory.CreateDirectory(_logFolderPath4);

            DirectoryInfo di = new DirectoryInfo(@"D:\Material\");
            bool time = false;
            using (var dest = File.AppendText(Path.Combine(_logFolderPath4, "Excel.txt")))
            {

                    if (!time)
                    {
                        dest.WriteLine("---------------------" + DateTime.Now + "---------------------");
                        dest.WriteLine("");
                        time = true;
                    }
                    CountFiles(dest, di, @"*.txt");
            }

    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T17:34:15+00:00Added an answer on May 22, 2026 at 5:34 pm

    You don’t necessarily need LINQ for this. It would be more efficient for you to just loop through it. Though Rup’s solution is a great use of LINQ here.

    Here’s a more complete version tailored for exactly what you want to do.

    // count it
    CountFiles(dest, di, @"*.xls");
    
    public void CountFiles(TextWriter writer, DirectoryInfo directory, string searchPattern)
    {
        var counter = new FileGroupCounter
        {
            { 5,    Multiplier.K },
            { 10,   Multiplier.K },
            { 20,   Multiplier.K },
            { 100,  Multiplier.K },
            { 1000, Multiplier.K },
            { 5,    Multiplier.M },
            { 10,   Multiplier.M },
            { 20,   Multiplier.M },
            { 50,   Multiplier.M },
            { 100,  Multiplier.M },
        };
    
        foreach (var file in directory.EnumerateFiles(searchPattern, SearchOption.AllDirectories))
                             // or use GetFiles() if you're not targeting .NET 4.0
        {
            counter.CountFile(file);
        }
    
        foreach (var result in counter)
        {
            writer.WriteLine("Excel File " + result);
            writer.WriteLine(result.Count);
            writer.WriteLine();
        }
    }
    
    // and the supporting classes
    public enum Multiplier : long
    {
        K = 1 << 10,
        M = 1 << 20,
        G = 1 << 30,
        T = 1 << 40,
    }
    
    public class FileGroupCounter : IEnumerable<FileGroupCounter.Result>
    {
        public ReadOnlyCollection<long> Limits { get { return roLimits; } }
        public ReadOnlyCollection<int> Counts { get { return roCounts; } }
        public ReadOnlyCollection<Multiplier> Multipliers { get { return roMultipliers; } }
    
        public FileGroupCounter()
        {
            limits = new List<long>();
            counts = new List<int>();
            multipliers = new List<Multiplier>();
            roLimits= limits.AsReadOnly();
            roCounts= counts.AsReadOnly();
            roMultipliers= multipliers.AsReadOnly();
        }
    
        private List<long> limits;
        private List<int> counts;
        private List<Multiplier> multipliers;
        private ReadOnlyCollection<long> roLimits;
        private ReadOnlyCollection<int> roCounts;
        private ReadOnlyCollection<Multiplier> roMultipliers;
    
        private long CalculateLength(int index)
        {
            return limits[index] * (long)multipliers[index];
        }
    
        public void Add(long limit, Multiplier multiplier)
        {
            int lastIndex = limits.Count - 1;
            if (lastIndex >= 0 && limit * (long)multiplier <= CalculateLength(lastIndex))
                throw new ArgumentOutOfRangeException("limit, multiplier", "must be added in increasing order");
    
            limits.Add(limit);
            counts.Add(0);
            multipliers.Add(multiplier);
        }
    
        public bool CountFile(FileInfo file)
        {
            if (file == null)
                throw new ArgumentNullException("file");
    
            for (int i = 0; i < limits.Count; i++)
            {
                if (file.Length <= CalculateLength(i))
                {
                    counts[i]++;
                    return true;
                }
            }
            return false;
        }
    
        public IEnumerator<Result> GetEnumerator()
        {
            for (int i = 0; i < limits.Count; i++)
            {
                if (counts[i] > 0)
                    yield return new Result(this, i);
            }
        }
        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { return GetEnumerator(); }
    
        public class Result
        {
            public long Limit { get { return counter.limits[index]; } }
            public int Count { get { return counter.counts[index]; } }
            public Multiplier Multiplier { get { return counter.multipliers[index]; } }
    
            internal Result(FileGroupCounter counter, int index)
            {
                this.counter = counter;
                this.index = index;
            }
            private FileGroupCounter counter;
            private int index;
    
            public override string ToString()
            {
                if (index > 0)
                    return String.Format("> {0} {1}B and <= {2} {3}B",
                        counter.limits[index - 1], counter.multipliers[index - 1],
                        counter.limits[index], counter.multipliers[index]);
                else
                    return String.Format("<= {0} {1}B",
                        counter.limits[index], counter.multipliers[index]);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I'm trying to simplify this code but can't think which way to do
I am trying to simplify this function at his maximum, how can I do?
I have now spent 2 days trying to get this going. I am however
My question is similar to this one , but I can simplify it some.
I'm trying to get some information from a web site. The information I want
So I am trying to delete from multiple tables (in this scenario 6). I
I'm trying to extend ScriptManager to simplify dealing with resources that have multiple resource
I'm trying to get two Ruby apps to work from the same port. I
I have 3 tables. For the purposes of this example I will simplify it
I am trying to simplify the build->archive->submit process for iOS app publishing. We have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.