Solved

File Search Algorithm

Posted on 1998-06-26
8
223 Views
Last Modified: 2010-04-02
Does anyone have a file Search algorithm I could use?  Example, I want to find mspaint.exe....I want to write a piece of code that would search for that file.  Thanks in advance.
0
Comment
Question by:mrquija
8 Comments
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1251441
Under Win32 you call FindFirstFile/FindNextFile .. these return files that match a given spec and sttributes in a given directory.

Search a directory tree by making the function recursive (ie. look for your particular file and if not fount retrieve all directory names and call the search for each of those.)

In Win32 you can also find an executable that is registered by API calls .. but this may not be what you want.

0
 
LVL 1

Author Comment

by:mrquija
ID: 1251442
I need a searching algorithm for DOS based, sorry.
0
 
LVL 1

Expert Comment

by:saneesh100
ID: 1251443
Hello..
 You want the dos one right. U can do it in the following way..

 Have u used findfirst and findnext ..or ucan use dos interrupts..

 start from c:\(root) and search for the file also add the directory names in an array .Got it. When the current is finished take the last one from directory array ( remove it also) add it to current path and re perform search .If one has nothing inside it .just remove one upto a '\'and again add form array.It will work..

 Please note: it will search from the last one >not like dir /s which will starts from first one..

 Good luck.

0
 
LVL 1

Author Comment

by:mrquija
ID: 1251444
haven't used those functions before.  Could you write code to implement?
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 3

Expert Comment

by:moonlight
ID: 1251445
I will give you an algorithm, the functions are for unix,
use functions in the earlier answers for dos:
 for (dp = readdir(d); dp != NULL;dp = readdir (d) )
   {
   filename = dp -> d_name;
   if ( filename[0] == '.' ) continue ;
   cout << filename <<  "\n";
   }

this is only for your help, not an answer
0
 
LVL 10

Accepted Solution

by:
RONSLOW earned 10 total points
ID: 1251446
something like this should work (haven't compiled or debugged it .. just typed it in)

char* WhereInThisDir(const char* szSpec, const char* szPath) {
      int lSpec,lPath;
    int lSubDirSpec;
    char* szSubDirSpec;
      int lFullSpec;
    char* szFullSpec;
      int lSubDir;
    char* szSubDir;
    struct _finddata_t aFindData;
    long hFileFind;
      char* szWhere;
   
    /* not found yet */
      szWhere = NULL;
   
    lSpec = strlen(szSpec);
    lPath = strlen(szPath);
   
    /* buffer for searching and returning file name */
    lFullSpec = lPath + 1 + lPath;
    szFullSpec = malloc(lFullSpec+1);
    strcpy(szFullSpec,szPath);
    strcat(szFullSpec,'\\');
    strcat(szFullSpec,szSpec);
   
    /* is it here? */
    hFileFind = _findfirst(szSpec, &aFindData );
    if (hFileFind != -1L ) szWhere = szFullSpec;
   
    /* no longer required */
    if (szWhere != szFullSpec) {
          free(szFullSpec);
    } else {
        /* we are returning the allocated string */
        /* caller needs to free it after use */
        /* similar to strdup */
    }
      _findclose(hFileFind);

    /* say where we found it (if at all) */
    return szWhere;
}

char* WhereInSubDir(const char* szSpec, const char* szPath) {
    int lSpec,lPath;
    int lSubDirSpec;
    char* szSubDirSpec;
    struct _finddata_t aFindData;
    long hFileFind;
      char* szWhere;
   
    /* not found yet */
      szWhere = NULL;
   
    lSpec = strlen(szSpec);
    lPath = strlen(szPath);

    /* make subdir spec for filefind */
      lSubDirSpec = lPath + 4;
      szSubDirSpec = malloc(lSubDirSpec+1);
      strcpy(szSubDirSpec,szPath);
      strcat(szSubDirSpec,'\\*.*');
   
    /* find subdirs */
      hFileFind = _findfirst(szSpec, &aFindData );
      if (hFileFind != -1L ) {
          do {
            /* found a szSubDir */
              if (aFindData.attrib & _A_SUBDIR) {
                  int lName;
                  char* szName;
                  int lSubDir;
                  char* szSubDir;
                  char* szWhere;
               
                  szName = aFindData.name;
                  lName = strlen(szName);

                /* make subdir path name */
                  lSubDir = lPath+1+lName;
                  szSubDir = malloc(lSubDir+1);
                  strcpy(szSubDir,szPath);
                  strcat(szSubDir,'\\');
                  strcat(szSubDir,szName);
               
                /* look in subdir tree */
                  szWhere = WhereInTree(szSpec,szSubDir);
               
                /* no longer required */
                  free(szSubDir);
            }
        } while(!szWhere && _findnext(hFileFind, &aFindData) == 0);
    }

    /* no longer required */
      free(szSubDirSpec);
      _findclose(hFileFind);
   
    /* say where we found it */
      return szWhere;
}

char* WhereInTree(const char* szSpec, const char* szPath) {
    /* returns NULL if not found */
    /* otherwise returns allocated string */
    /* caller needs to free the string */
      const char* szWhere;
   
      szWhere = WhereInThisDir(szSpec,szPath);
      if (! szWhere) szWhere = WhereInSubDir(szSpec,szPath);
      return szWhere;
}

0
 
LVL 1

Author Comment

by:mrquija
ID: 1251447
Im not sure if that code is going to compile either.  Seems too complicated for a file search algorithm?!?   But I'll give it a shot.  If anyone has anything SHORTER, it would be appreciated.
0
 
LVL 10

Expert Comment

by:RONSLOW
ID: 1251448
The main reason for the length is the lengths (sic) you have to go to to set up a findfirst/findnext loop .. mostly the string manipulation.

You could used fixed size buffers to make it a bit simpler ,, as long as you make the buffers big enough.

The alrogithm itself is quite simply .. just the details get in the way.

0

Featured Post

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now