搜索目录的递归方法,如果找到搜索名称,则返回true [英] Recursive method to search for a directory and return true if the search name is found

查看:87
本文介绍了搜索目录的递归方法,如果找到搜索名称,则返回true的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个搜索根目录的函数,如果在根目录中找到具有特定名称的目录,则返回true.

我无法使它正常工作,即使它找到具有相同名称的目录,它也总是返回false-我在做什么错-请! (我的额头很疼)

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;
} 

推荐答案



您必须从递归中获取结果.

试试:

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;
}.



接下来,您可能会认真考虑是否进行区分大小写的搜索.如果在参数列表中添加另一个布尔(ignoreCase),甚至可以将其留给用户.

希望这对您有用.

干杯,AT



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


CString中的Compare方法区分大小写.

"a"和"A"被视为不同的字符.

尝试CompareNoCase().

如果这还不够,请与调试器一起进行遍历,看看代码如何分析数据,看看哪里出错了.
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.


这篇关于搜索目录的递归方法,如果找到搜索名称,则返回true的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆