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 766479
In Process

The Archive Base Latest Questions

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

I have program which writes to database which folders are full or empty. Now

  • 0

I have program which writes to database which folders are full or empty. Now I’m using

bool hasFiles=false;
(Directory.GetFiles(path).Length >0) ? hasFiles=true: hasFiles=false;

but it takes almost one hour, and I can’t do anything in this time.

Is there any fastest way to check if folder has any file ?

  • 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-14T17:03:17+00:00Added an answer on May 14, 2026 at 5:03 pm

    The key to speeding up such a cross-network search is to cut down the number of requests across the network. Rather than getting all the directories, and then checking each for files, try and get everything from one call.

    In .NET 3.5 there is no one method to recursively get all files and folders, so you have to build it yourself (see below). In .NET 4 new overloads exist to to this in one step.

    Using DirectoryInfo one also gets information on whether the returned name is a file or directory, which cuts down calls as well.

    This means splitting a list of all the directories and files becomes something like this:

    struct AllDirectories {
      public List<string> DirectoriesWithoutFiles { get; set; }
      public List<string> DirectoriesWithFiles { get; set; }
    }
    
    static class FileSystemScanner {
      public AllDirectories DivideDirectories(string startingPath) {
        var startingDir = new DirectoryInfo(startingPath);
    
        // allContent IList<FileSystemInfo>
        var allContent = GetAllFileSystemObjects(startingDir);
        var allFiles = allContent.Where(f => !(f.Attributes & FileAttributes.Directory))
                                 .Cast<FileInfo>();
        var dirs = allContent.Where(f => (f.Attributes & FileAttributes.Directory))
                             .Cast<DirectoryInfo>();
        var allDirs = new SortedList<DirectoryInfo>(dirs, new FileSystemInfoComparer());
    
        var res = new AllDirectories {
          DirectoriesWithFiles = new List<string>()
        };
        foreach (var file in allFiles) {
          var dirName = Path.GetDirectoryName(file.Name);
          if (allDirs.Remove(dirName)) {
            // Was removed, so first time this dir name seen.
            res.DirectoriesWithFiles.Add(dirName);
          }
        }
        // allDirs now just contains directories without files
        res.DirectoriesWithoutFiles = new List<String>(addDirs.Select(d => d.Name));
      }
    
      class FileSystemInfoComparer : IComparer<FileSystemInfo> {
        public int Compare(FileSystemInfo l, FileSystemInfo r) {
          return String.Compare(l.Name, r.Name, StringComparison.OrdinalIgnoreCase);
        }
      }
    }
    

    Implementing GetAllFileSystemObjects depends on the .NET version. On .NET 4 it is very easy:

    ILIst<FileSystemInfo> GetAllFileSystemObjects(DirectoryInfo root) {
      return root.GetFileSystemInfos("*.*", SearchOptions.AllDirectories);
    }
    

    On earlier versions a little more work is needed:

    ILIst<FileSystemInfo> GetAllFileSystemObjects(DirectoryInfo root) {
      var res = new List<FileSystemInfo>();
      var pending = new Queue<DirectoryInfo>(new [] { root });
    
      while (pending.Count > 0) {
        var dir = pending.Dequeue();
        var content = dir.GetFileSystemInfos();
        res.AddRange(content);
        foreach (var dir in content.Where(f => (f.Attributes & FileAttributes.Directory))
                                   .Cast<DirectoryInfo>()) {
          pending.Enqueue(dir);
        }
      }
    
      return res;
    }
    

    This approach calls into the filesystem as few times as possible, just once on .NET 4 or once per directory on earlier versions, allowing the network client and server to minimise the number of underlying filesystem calls and network round trips.

    Getting FileSystemInfo instances has the disadvantage of needing multiple file system operations (I believe this is somewhat OS dependent), but for each name any solution needs to know if it is a file or directory so this is not avoidable at some level (without resorting to P/Invoke of FindFileFirst/FindNextFile/FindClose).


    Aside, the above would be easier with a partition extension method:

    Tuple<IEnumerable<T>,IEnumerable<T>> Extensions.Partition<T>(
                                                     this IEnumerable<T> input,
                                                     Func<T,bool> parition);
    

    Writing that to be lazy would be an interesting exercise (only consuming input when something iterates over one of the outputs, while buffering the other).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 477k
  • Answers 477k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Turns out it was a bug. See the edited question… May 16, 2026 at 5:06 am
  • Editorial Team
    Editorial Team added an answer The warning is only because the compiler doesn't know if… May 16, 2026 at 5:06 am
  • Editorial Team
    Editorial Team added an answer The previous answers provide a part of the solution, but… May 16, 2026 at 5:06 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.