Click here to Skip to main content
15,905,607 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to write a function to search a root directory and return true if a directory is found with a certain name within the root directory.

I cannot get it to work, it always returns false, even if it finds the directory with the same name - what am I doing wrong - please!!! (my forehead is sore)

bool NameIsInDirectory(const CString& rootPath, const CString& searchName)const
{ 
   CFileFind finder;
   CString folderName;
   CString folderPath;
    
   // build a string with wildcards 
   CString strWildcard(rootPath); 
   strWildcard += _T("\\*.*"); 
 
   // start working for files
   BOOL bWorking = finder.FindFile(strWildcard);
   
   while (bWorking)
   {
	   bWorking = finder.FindNextFile();
	   
	   // skip . and .. files; otherwise, we'd recur infinitely
	   if (finder.IsDots()) 
		   continue;
	   
	   // if it's a directory, recursively search it
	   if (finder.IsDirectory())
	   {
		   CString path = finder.GetFilePath();
		   CString name = finder.GetFileName();
		   if(!name.Compare(searchName))
		   {
			   //bFound =  true;
			   return true;
		   }
		   NameIsInDirectory(path, searchName); 
	   }
   } 
   finder.Close();
   return false;
} 
Posted

Hi,

You've got to take the result back from the recursion.

try:

bool NameIsInDirectory(const CString& rootPath, const CString& searchName)const
{ 
   bool result = false;
   CFileFind finder;
   CString folderName;
   CString folderPath;
    
   // build a string with wildcards 
   CString strWildcard(rootPath); 
   strWildcard += _T("\\*.*"); 
 
   // start working for files
   BOOL bWorking = finder.FindFile(strWildcard);
   
   while (bWorking && ! result)
   {
	   bWorking = finder.FindNextFile();
	   
	   // skip . and .. files; otherwise, we'd recur infinitely
	   if (finder.IsDots()) 
		   continue;
	   
	   // if it's a directory, recursively search it
	   if (finder.IsDirectory())
	   {
		   CString path = finder.GetFilePath();
		   CString name = finder.GetFileName();
		   if(!name.Compare(searchName))
		   {
			   //bFound =  true;
			   result = true;
                            break;
		   }
		   result = NameIsInDirectory(path, searchName); 
	   }
   } 
   finder.Close();
   return result;
}.


Next to that you might take a serious look at doing a case sensitive search or not. you could even leave that to the user if you add another bool (ignoreCase) to parameter list.

Hope this works for you.

Cheers, AT
 
Share this answer
 
v2
Comments
Jackie Lloyd 15-Dec-11 11:37am    
That's super - thankyou, it worked. I tried so many things a bit like you suggested but just didn't get it quite right.
The Compare method in CString is case sensitive.

'a' and 'A' are seen as different characters.

Try CompareNoCase().

If that isn't enough of a change, walk through it with the debugger and see how the code is analyzing the data, and see where it goes wrong.
 
Share this answer
 
Comments
Jackie Lloyd 15-Dec-11 11:17am    
Hi, thanks again, you are very helpful. But it is not the case that is the problem It finds the directory fine, and when I was using the flag bFound, it sets it to true. It stays true when I carry on stepping through with the debugger then it gets reset to false at the end.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900