cap7
cap7

Reputation: 522

GetFiles is too slow on a Network Drive

I need to copy on a daily basis files from a Network Drive. In order to do that I have tried the following:

    var dir = new DirectoryInfo(@"Z:\");

    var filesA300 = dir.GetFiles().Where(x => x.FullName.Contains("A300") 
&& x.LastWriteTime.Date == DateTime.Now.Date).ToList();

Since the drive has thousands of files the program doesn't do anything in a useful period of time.

What are my alternatives?

Upvotes: 2

Views: 2562

Answers (2)

Sayse
Sayse

Reputation: 43330

GetFiles returns all the files to begin with before you start to filter them,

You can use EnumerateFiles which is lazier and lets you chain your where query. You can also filter on specific file types

Quote from the above link

The EnumerateFiles and GetFiles methods differ as follows:

  • When you use EnumerateFiles, you can start enumerating the collection of FileInfo objects before the whole collection is returned.

  • When you use GetFiles, you must wait for the whole array of FileInfo objects to be returned before you can access the array.

You may also be better off doing this operation on a seperate thread so you don't block your main application thread, although this would depend on what else the application is designed to do (still may be worth while so you can stop it thinking it is unresponsive).

Upvotes: 3

Ognyan Dimitrov
Ognyan Dimitrov

Reputation: 6273

You can enumerate on the file infos first and then get only what you want.

var myFilesToProcessInfos =  new DirectoryInfo("your location").EnumerateFileSystemInfos("*", SearchOption.TopDirectoryOnly).Where(x=>x.Name.Contains("Your pattern") /*&& x.CreationTime == your pattern*/);
            foreach (FileInfo fInfo in myFilesToProcessInfos)
            {
                // do your stuff
            }

Upvotes: 1

Related Questions