Author Topic: [C] FileSearch  (Read 1724 times)

0 Members and 4 Guests are viewing this topic.

Offline colton

  • NULL
  • Posts: 1
  • Cookies: 0
    • View Profile
[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;
}
« Last Edit: August 11, 2014, 03:46:59 am by colton »

Offline s3my0n

  • Knight
  • **
  • Posts: 276
  • Cookies: 58
    • View Profile
    • ::1
Re: [C] FileSearch
« Reply #1 on: August 14, 2014, 11:53:51 am »
Can be faster if instead of recursion you use iteration. Can also crash if there's too many nested directories - runs out of stack space.
Easter egg in all *nix systems: E(){ E|E& };E

Offline Sherlock Holmes

  • Peasant
  • *
  • Posts: 97
  • Cookies: 4
    • View Profile
Re: [C] FileSearch
« Reply #2 on: August 17, 2014, 03:20:42 pm »
CodeBlocks gives error on line 43..


C:\CodeBlocks\FileSearch\main.c|43|error: 'struct dirent' has no member named 'd_type'

Offline Kulverstukas

  • Administrator
  • Zeus
  • *
  • Posts: 6627
  • Cookies: 542
  • Fascist dictator
    • View Profile
    • My blog
Re: [C] FileSearch
« Reply #3 on: August 17, 2014, 09:53:03 pm »
dirent is probably an external lib which you need to link.

Offline institute

  • NULL
  • Posts: 2
  • Cookies: 0
    • View Profile
Re: [C] FileSearch
« Reply #4 on: August 18, 2014, 06:46:46 am »
You can get the dirent.h here, http://softagalleria.net/dirent.php.


Just drop it in your includes.

Offline kenjoe41

  • Symphorophiliac Programmer
  • Administrator
  • Baron
  • *
  • Posts: 990
  • Cookies: 224
    • View Profile
Re: [C] FileSearch
« Reply #5 on: September 04, 2014, 06:14:03 pm »
And i thought dirent.h is a unix header. Correct me if it actually works on windows.
If you can't explain it to a 6 year old, you don't understand it yourself.
http://upload.alpha.evilzone.org/index.php?page=img&img=GwkGGneGR7Pl222zVGmNTjerkhkYNGtBuiYXkpyNv4ScOAWQu0-Y8[<NgGw/hsq]>EvbQrOrousk[/img]

Offline TheWormKill

  • EZ's Scripting Whore
  • Global Moderator
  • Knight
  • *
  • Posts: 257
  • Cookies: 66
  • The Grim Reaper of Worms
    • View Profile
Re: [C] FileSearch
« Reply #6 on: September 04, 2014, 09:54:08 pm »
Just adding something that might be important:
If you use optimization (which is a probably good idea in this specific case if we're talking about performance), many compilers will convert recursive structures to loops if possible. Just sayin' ^^.
Stuff I did: How to think like a superuser, Iridium

He should make that "Haskell"
Quote
<m0rph-is-gay> fuck you thewormkill you python coding mother fucker

Offline Sherlock Holmes

  • Peasant
  • *
  • Posts: 97
  • Cookies: 4
    • View Profile
Re: [C] FileSearch
« Reply #7 on: September 14, 2014, 06:57:01 pm »
And i thought dirent.h is a unix header. Correct me if it actually works on windows.

Didn't work for me on windows..included the dirent file too...idk..maybe i did something wrong....?

Offline Kulverstukas

  • Administrator
  • Zeus
  • *
  • Posts: 6627
  • Cookies: 542
  • Fascist dictator
    • View Profile
    • My blog
Re: [C] FileSearch
« Reply #8 on: September 14, 2014, 09:16:09 pm »
Didn't work for me on windows..included the dirent file too...idk..maybe i did something wrong....?
stacktrace pl0x.