Search code examples
c#.netsearchdirectorydirectorysearcher

Fast way to check existance of a file in a directory with filename


I have a physical Directory structure as :

Root directory (X) -> many subdirectory in side root (1,2,3,4..) -> In each sub dir many files present.

Photos(Root)    
            ----        
               123456789(Child One)
                 ----
                     1234567891_w.jpg (Child two)
                     1234567891_w1.jpg(Child two)
                     1234567891_w2.jpg(Child two)
                     1234567892_w.jpg (Child two)
                     1234567892_w1.jpg(Child two)
                     1234567892_w2.jpg(Child two)
                     1234567893_w.jpg(Child two)
                     1234567893_w1.jpg(Child two)
                     1234567893_w2.jpg(Child two)
                     -----Cont      
              232344343(Child One)      
              323233434(Child One)      
              232323242(Child One)      
              232324242(Child One)      
              ----Cont..

In database I have one table having huge number of names of type "1234567891_w.jpg".

NOTE : Both number of data in database and number of photos are in lacs.

I need an effective and faster way to check the presence of each name from database table to the physical directory structure.

  • Ex : Whether any file with "1234567891_w.jpg" name is present in physical folder inside Photos (Root).*

Please let me know if I miss any information to be given here.

Update :

I know how to find a file name existance in a directory. But I am looking for an efficient way, as it will be too much resource consuming to check each filename (from lacs of record) existance in more than 40 GB data.


Solution

  • It might sound funny or Might be I was unclear or did not provide much information..

    But from the directory pattern I got one nice way to handle it is :

    AS the probability of existance of the file name is only in one location and that is :

    Root/SubDir/filename

    I should be using :

    File.Exists(Root/SubDir/filename);

    i.e - Photos/123456789/1234567891_w.jpg

    And I think this will be O(1)