0

Retrieve all .doc files exist in My Computer using a search term which user provides. I have tried to query systemindex catlog and the problem is, it is not fetching the documents which is recently added.

My code is like below

SELECT "System.ItemName", "System.ItemFolderPathDisplay" FROM "SystemIndex" WHERE CONTAINS(*,'"searchterm"',1033) AND (System.FileName LIKE '%.doc' OR System.FileName LIKE '%.txt') AND Contains(System.Kind, 'document') ORDER BY System.FileName ASC

The problem with the above query is, it is not fetching instantly created files sometimes.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
user3366358
  • 23
  • 1
  • 6

2 Answers2

1

I use the following code to get all .doc's on C:. It can be modified to search for a user specified string. You could just use this and then loop through filePaths, looking for your user specified string.

    string myPath = @"C:\";
    string[] filePaths = Directory.GetFiles(myPath, "*.doc",SearchOption.AllDirectories);

Edit: of course, this eliminates the need for a wmi query.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
riki-oh
  • 63
  • 2
  • 8
  • I have tried this route and it is not working out. If i have 500 documents in my C drive, it takes forever :( – user3366358 Mar 06 '14 at 21:49
  • Well it is hard to get current results without doing it this way. Maybe setup an indexed database? Nah. Maybe limit the search, exclude C:\Windows etc. – riki-oh Mar 06 '14 at 22:22
  • Why not kick this search off as soon in another thread and have the results ready for the user to fill in data and submit? – Nick H. Mar 06 '14 at 23:50
0

I would use .net's Directory class.

var files = Directory.GetFiles(path, "*.doc", SearchOptions.AllDirectories)
               .Where(m => m.Contains(yourSearchTerm));

That'll return all .doc files in the directory at the path you provide that contain the given search term. If its too slow I'd look into limiting it from running on the entire c drive.

More info about the Directory class can be found here

More info about Enumerable's Where method can be found here

Edit: To handle UnauthorizedAccessExceptions you need to do this recursively and check each directory individually so you can eat the exception if you don't have access to the current directory.

IEnumerable<string> SearchAccessibleFiles(string root, string searchTerm) {
    var files = new List<string>();

    foreach (var file in Directory.GetFiles(root)
                             .Where(m => m.Contains(searchTerm))) 
    {
        files.Add(file);
    }
    foreach (var subDir in Directory.GetDirectories(root)) {
        try {
            files.AddRange(GetAllAccessibleFiles(subDir, searchTerm));
        }
        catch (UnauthorizedAccessException ex) {
            // ...
        }
    }

    return files;
} 

And can be used like:

var files = GetAllAccessibleFiles(@"c:\", "bugs");

This will return every file in an accessible directory that contains the phrase "bugs" in the file name.

Neil Smith
  • 2,565
  • 1
  • 15
  • 18
  • Some folders like Documents & Settings throws error with UnauthorizedAccessException. How to fix that? – user3366358 Mar 06 '14 at 22:40
  • Do you want to ignore the folders you don't have access to? Or do you still want to search through them? – Neil Smith Mar 06 '14 at 22:43
  • I want to search through all the folders if possible. – user3366358 Mar 07 '14 at 01:17
  • I really don't know how to search through files you don't have access to. I'm sure you can set your app to always run as administrator. I tried Yochai Timmer's answer [here](http://stackoverflow.com/questions/4986293/access-to-the-path-is-denied-when-using-directory-getfiles) but ended up with more problems. I'm editing my answer with a recursive function I wrote to get all accessible files. I know it's not ideal for you but it's all I have now. I'll keep thinking about this when I get home though so I might come up with something later. – Neil Smith Mar 07 '14 at 02:01
  • Thanks a lot for your continuous help. GetAllAccessibleFiles is not being recognized in my code. Do you meant GetAllAccessibleFiles as SearchAccessibleFiles? I want to fetch files from all drives from the computer and i need to fetch more than one extension like ".doc",".pdf" Kindly help. Thanks a lot for your help and time in advance. – user3366358 Mar 07 '14 at 02:35
  • Yea sorry about that. Was a typo. Switch GetAllAccessibleFiles with SearchAccessibleFiles. I changed the name after writing the function and forgot to change the line where it calls itself. The function currently searches through all file types. I'll let you edit it if you want to limit it to given extensions. Also, if you wanted it to search every drive, you'd have to call it multiple times passing in each available drive on your computer which you can get from DriveInfo.GetDrives(). – Neil Smith Mar 07 '14 at 16:49
  • Thanks. I will try to loop for every drive. Did you get any luck to fix parsing on access denied folders? – user3366358 Mar 07 '14 at 18:34
  • I didn't. There's a lot of stackoverflow questions about it but they all just talk about properly handling the UnauthorizedAccessException properly so that you can keep searching for files you do have access for without the exception breaking everything. Look into forcing your app to run as administrator. I believe that should allow it access to everything. – Neil Smith Mar 07 '14 at 18:50
  • If possible, please post handling the UnauthorizedAccessException properly code. i am getting some error there. – user3366358 Mar 07 '14 at 19:09
  • It does catch the exception. Are you sure its throwing an UnauthorizedAccesException? When that is thrown, it should just skip the directory that you don't have access to and silently continue to the next directory. I can't tell you how to handle the exception because I don't know what you want to do with it. What exception are you getting? You'll have to add exception handlers for the different kinds of exceptions that can be thrown. – Neil Smith Mar 07 '14 at 19:21