Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - colton

Pages: [1]
1
C - C++ / [C] FileSearch
« on: August 11, 2014, 03:46:20 am »
Here's an example of file search using the dirent.h, it works well. Way faster then the built in Windows 7 search... I was impressed and found this on accident.

Code: (c) [Select]
#include <stdio.h>
#include <dirent.h>
#include <time.h>


int FileSearch(char* szPath, char* szSearch);


int main( int argc, char* argv[])
{
   clock_t b, s;

   b=clock();

   if (argc != 3) {
      fprintf(stderr, "Usage: FileSearch <directory> <search/term>\r\n");
   }
   FileSearch(argv[1], argv[2]);

   s=clock();

   printf("Elapsed: %f seconds\n", (double)(s - b) / CLOCKS_PER_SEC);

   return 0;

}
int FileSearch(char* szPath, char* szSearch)
{
   struct dirent* entry;
   char szBuffer[260];
   DIR *dir;


   if ((dir = opendir(szPath)) == NULL) {
      return 1;
   }
   while ((entry = readdir(dir)))
   {
      if (strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0)
      {
         sprintf_s(szBuffer, sizeof(szBuffer), "%s%s", szPath, entry->d_name);

         if (S_ISDIR(entry->d_type)) {
            strcat_s(szBuffer, "\\");
            FileSearch(szBuffer, szSearch);
         }
         if (strstr(entry->d_name, szSearch)) {
            printf("Found match: %s\r\n", szBuffer);
         }
      }
   }
   closedir(dir);

   return 0;
}

Pages: [1]