Thread: Searching recursive(with code)

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    76

    Searching recursive(with code)

    I needed to search for a single file, in a particular directory. Although there are plenty of examples, most examples are pretty big to just find a file. So i decided to make my own. Im posting it since i am not shure if this done the right way. Would this work an all win versions? For every user?? If its correct, i hope other might be able to use it.
    Code:
    // searches recursive for fileName in dir and if fileName is found, the fullpath is copied into fileName
    bool SearchFile(char dir[FILENAMESIZE],char fileName[FILENAMESIZE])
    {
    	WIN32_FIND_DATA fd;
    	char txt[FILENAMESIZE];
    	sprintf(txt,"%s%s",dir,fileName);
    	HANDLE hFind = FindFirstFile(txt,&fd);
    	FindClose(hFind);
    	if(hFind != INVALID_HANDLE_VALUE)
    	{
    		sprintf(fileName,"%s%s",dir,fd.cFileName);
    		return true;
    	}
    	sprintf(txt,"%s*",dir);
    	hFind = FindFirstFile(txt,&fd);
    	while(FindNextFile(hFind,&fd))
    	{
    		if((fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) && strcmp(fd.cFileName,"..") != 0)
    		{
    			sprintf(txt,"%s%s\\",dir,fd.cFileName);
    			if(SearchFile(txt,fileName))
    			{
    				FindClose(hFind);
    				return true;
    			}
    		}
    	}
    	FindClose(hFind);
    	return false;
    }
    you use it like this:
    Code:
    void FindSomeFile()
    {
        char dir[FILENAMESIZE];
        char fileName[FILENAMESIZE];
        strcpy(dir,"C:\\");
        strcpy(fileName,"p*.exe");
        if(SearchFile(dir,fileName))
        {showMsg(fileName);}
        else
        {}
    }
    Last edited by johny145; 06-16-2005 at 11:11 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 12:38 PM
  2. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  3. Searching a linked list for char
    By spentdome in forum C Programming
    Replies: 3
    Last Post: 05-22-2002, 11:11 AM
  4. searching linked lists
    By spentdome in forum C Programming
    Replies: 1
    Last Post: 04-21-2002, 06:46 PM
  5. Replies: 4
    Last Post: 01-16-2002, 12:04 AM