0

I want to find all the document files from D drive . I have written the following code but it is too time consuming. Also due to more time consumption in fetching the data it gives me exception. In order to minimize the time ,I just want to check like this :

even if one of the types of file exist within that folder ->return the path of the folder

so that it doesn't need to check all the type of files within that same folder example:-

if C:\Program Files\Notepad++ has readme.txt ,and other text files. The moment it detects the first text file in this folder it must return : C:\Program Files\Notepad++

Code that I have used in my file

 List<string> mediaExtensions = new List<string>{"txt", "pdf"};
    List<string> filesFound = new List<string>();

    void DirSearch(string sDir) 
    {
       foreach (string d in Directory.GetDirectories(sDir)) 
       {
        foreach (string f in Directory.GetFiles(d, "*.*")) 
        {
            if(mediaExtensions.Contains(Path.GetExtension(f).ToLower()))
               filesFound.Add(f);
        }
        DirSearch(d);
       }
    }

Update1: Checking this link did gave me an idea of recurssion and ignoring the files but not minimizing the time taken to search the files.

Update2:- In the above code that I used if(mediaExtensions.Contains(Path.GetExtension(f).ToLower())) I dont get any value even if there exists files within the folder

Community
  • 1
  • 1
TechBrkTru
  • 346
  • 1
  • 25
  • Look [here](http://stackoverflow.com/questions/1199260/file-exists-by-file-name-pattern) for this option: `bool exist = Directory.EnumerateFiles(path, "*_peach.xml").Any();` Using any should make it faster. Let us know if it does! – TaW Jun 09 '15 at 10:56
  • When at least one file is found, add the Directory into a list and use a break to stop searching in that directory. – Olaru Mircea Jun 09 '15 at 10:57

4 Answers4

1

First Solution: (.Net 4.0)
This could speed things up a bit:

    DirectoryInfo dirInfo = new DirectoryInfo(myBaseDirectory);
    return dirInfo.EnumerateDirectories()
           .AsParallel()
           .SelectMany(di => di.EnumerateFiles("*.*", SearchOption.AllDirectories));
}

Second Solution: (.Net 3.5 and 4.0)
If that`s not helping check out the Faster Directory Enumerator based on a WinAPI function

Sources: http://www.codeproject.com/Articles/38959/A-Faster-Directory-Enumerator
Win Api Function: https://msdn.microsoft.com/en-us/library/aa364428%28v=vs.85%29.aspx

Marc Wittmann
  • 2,286
  • 2
  • 28
  • 41
  • I can't use this EnumerateDirectories because I'm using .net 3.5,Is there any oher way @Marc Wittmann – TechBrkTru Jun 09 '15 at 13:00
  • Did you check the link for the FasterDirectory enumeration? As states this uses normal Windows API.. and it should be fast! DirectoryInfo.GetFiles method: ~230ms | FastDirectoryEnumerator.GetFiles method: ~33ms – Marc Wittmann Jun 09 '15 at 13:23
  • yes I checked them out ,and it seems to get faster result,but i have too many other things to add with it...also `EnumerateDirectories ` is accesssible only in net 4.0 @@Marc Wittmann – TechBrkTru Jun 09 '15 at 13:26
  • The class name is "FastDirectoryEnumerator", keep in mind it`s not the .Net one! The project itself is a 3.5 .Net Project and should be quiet easy to implement (not so much code because it uses the windows API). TheSource ocde is available at the link provided – Marc Wittmann Jun 09 '15 at 14:12
  • +1 for the AsParallel. But what is important to mention, is that your code does not return the files in the topdirectory, they have to be searched in an extra call to EnumerateFiles. – Philm Mar 03 '17 at 10:07
0

You can use the Directory.GetFiles() method to find files recursively. Check the options, you can adapt it to fit your needs without iterating the directories yourself:

https://msdn.microsoft.com/en-us/library/ms143316.aspx

Rob
  • 11,492
  • 14
  • 59
  • 94
0

Use this:

foreach (string f in Directory.GetFiles(d, "*.txt")) 
{
    // return ".txt file found in d
DrKoch
  • 9,556
  • 2
  • 34
  • 43
0
List<string> mediaExtensions = new List<string>{"txt", "pdf"};
List<string> filesFound = new List<string>();

void DirSearch(string sDir) 
{
   foreach (string d in Directory.GetDirectories(sDir)) 
   {
    foreach (string f in Directory.GetFiles(d, "*.*")) 
    {
        if(mediaExtensions.Contains(Path.GetExtension(f).ToLower()))
           filesFound.Add(f);
           //you said you also need the path of the directory, add it from here
           break;
    }
    DirSearch(d);
   }
}

When you add the f argument, try to also add the d one from the outer foreach, your requirement was to add the folder with at least one file with the desired extension exists.

EDIT 1

When you encounter Documents and settings you have to access it using

var mydocumentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
Olaru Mircea
  • 2,570
  • 26
  • 49
  • I get an unauthorized exception in executing the above code.cant those files/folder be skipped or store them in another folder path array to know that these files/folders had been given access denial??? @Olaru Mircea – TechBrkTru Jun 09 '15 at 11:16
  • I am curious, do you get the same result when running the application with admin right? Run the .exe with the option Run as administrator. – Olaru Mircea Jun 09 '15 at 11:34
  • yes even doing like that i get exception denied for documents and settings in C:\ – TechBrkTru Jun 09 '15 at 11:44
  • http://stackoverflow.com/questions/8821410/system-unauthorizedaccessexception-access-to-the-path-denied check this post for the same Unauthorized issue. – Olaru Mircea Jun 09 '15 at 11:52
  • check out this [link](https://www.dropbox.com/s/iqiwhej437xb5he/2%20%282%29.png?dl=0) – TechBrkTru Jun 09 '15 at 11:56
  • well unauthorized link wasn't that helpful @ Olaru Mircea – TechBrkTru Jun 09 '15 at 12:47
  • @TechBrkTru check my edit on the answer. You need to give special attention to that Documents and settings directory. – Olaru Mircea Jun 09 '15 at 12:50
  • I may encounter like that with many other files later,so what could be done then??@ Olaru Mircea – TechBrkTru Jun 09 '15 at 12:53
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/80059/discussion-between-techbrktru-and-olaru-mircea). – TechBrkTru Jun 09 '15 at 12:55