Windows有没有判断某目录下是否存在子目录的函数啊?

kingeboy 2006-05-24 01:59:44
有没有一个函数直接可以判断目录下是否有子目录的,不需要递归方法来获取.
...全文
631 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
kingeboy 2006-05-25
  • 打赏
  • 举报
回复
谢谢keiy的回复,这个方法我的程序段也已经实现了,看来还是没办法实现不用遍历文件来确定是否存在子目录的方法。
现在想问个问题就是,搜索文件的时候能否设置搜索排序(就是搜索的时候就已经按顺序搜索,不是后期手工处理排序的),比如按名称排序或按类型排序,这样就好先find出文件夹啦,因为现在用FindFirst,和findnext查找的时候不是按照类型来排序而是按照名字来排序,所以必须要find到最后一个文件才知道子目录有没有找完。
柯本 2006-05-24
  • 打赏
  • 举报
回复
//如果只要判一目录下是否有子目录.
#include <dir.h>
...
void __fastcall TForm1::Button1Click(TObject *Sender)
{
struct ffblk ffblk;
int done,havedir;
havedir=0;
done = findfirst("d:\\temp\\*.*",&ffblk,FA_DIREC);
while (!done)
{
if (ffblk.ff_attrib==FA_DIREC)
if ((strcmp(ffblk.ff_name,".")!=0) && (strcmp(ffblk.ff_name,"..")!=0))
{
havedir=1;
break;
}
done = findnext(&ffblk);
}
if (havedir==1)
ShowMessage("有子目录");
else
ShowMessage("无子目录");

}
kingeboy 2006-05-24
  • 打赏
  • 举报
回复
我现在想实现的功能是:我有一个文件名的列表和一些公用目录(是存在有子目录的),然后用程序从这些公用目录中查找出我所需的文件,因为要考虑到子目录所以也加入了遍历子目录方式,我用了两个FindFirst函数来搜索文件和文件夹,但是在一个目录中就要执行两次,先搜索一次有没有特定文件,然后再FindNext来查找是否有子目录.
所以就想问有没有一个函数可以直接判断某个目录下有没有任何子目录存在,存在就返回true,还是把我写的遍历子目录函数贴出来,大家看看有没有什么地方好修改的,实际上这个函数搜索速度我觉得已经很快了
String SearchFolder(String strPath,String strFileName)
{
TSearchRec sr,srDir;
memset(&sr,'\0',sizeof(TSearchRec));
memset(&srDir,'\0',sizeof(TSearchRec));
HANDLE hFind;
int nFind,nDirFind;
bool bFind=false,bFindDir=false;

String PathBak,PathOld,RealPath,Result,sFileName;

//复制初始用户选择目录
PathBak=strPath;
//寻找第一个文件
sFileName=strFileName.SubString(0,strFileName.Length()-ExtractFileExt(strFileName).Length());
RealPath=strPath+"\\"+sFileName+".*";
nFind=FindFirst(RealPath,faAnyFile,sr);

RealPath=strPath+"\\*";
nDirFind=FindFirst(RealPath,faDirectory,srDir);
Application->ProcessMessages();
//搜索所有文件及子目录
do
{
if(nFind==0)
{
Application->ProcessMessages();
//恢复初始用户选择目录

strPath=PathBak;
//列出所有发现的文件
strPath=strPath+"\\"+sr.Name;
if(FileExists(strPath)) //找到文件
{
Result=ExtractFileName(strPath).UpperCase();

if(Result==strFileName.UpperCase())())//如果找到的文件等于要找的文件
{
bFind=bFindDir=true;
break;
}
else
nFind=FindNext(sr);
}
}
else if(nDirFind==0)
{
//如果是当前目录或父目录,跳过
if(srDir.Name=="."||srDir.Name=="..")
{bFindDir=FindNext(srDir);}
else
{
if((srDir.Attr & faDirectory) != srDir.Attr)
{
bFindDir=FindNext(srDir);
continue;
}
strPath=PathBak+"\\"+srDir.Name;
RealPath=strPath+"\\"+sFileName+".*";
SetCurrentDirectory(strPath.c_str());
nFind=FindFirst(RealPath,faAnyFile,sr);
if(nFind!=0)
{
strPath=SearchFolder(strPath,strFileName);
if(strPath.IsEmpty())
bFindDir=FindNext(srDir);
else
{
bFindDir=bFind=true;
strPath="";
}
}
else
{
PathBak=strPath;
bFind=true;
}
}
}
else
{
bFindDir=bFind=true;
strPath="";
}
}
while(!bFindDir);
FindClose(sr);
FindClose(srDir);
if(!bFind)
strPath="";
return strPath;
}
//---------------------------------------------------------------------------
cczlp 2006-05-24
  • 打赏
  • 举报
回复
FindFirst后需要 FindClose, 这是2个API.
楼主说是用一个
kingeboy 2006-05-24
  • 打赏
  • 举报
回复
如果目录中有很多文件用FindFirst 和 FindNext 就不能保证很快速的判断,必须要把文件全部查找完了才能知道的啊
kingeboy 2006-05-24
  • 打赏
  • 举报
回复
Keiy能否说的具体一点比入我要判断c:\123目录下面有没有存在子目录改怎么写
我试了一下老是不成功
玄之丞 2006-05-24
  • 打赏
  • 举报
回复
使用FindFirst 和 FindNext ,去除 . 與 ..
判定一下類型,就可以了.
柯本 2006-05-24
  • 打赏
  • 举报
回复
如果只是要判当前目录下有无子目录,可用int findfirst(const char *pathname, struct ffblk *ffblk, int attrib);将attriby设为FA_DIREC就可以
L_75480035 2006-05-24
  • 打赏
  • 举报
回复
void __fastcall TForm1::Button1Click(TObject *Sender)
{
AnsiString TempDoc = "";
if(SelectDirectory("Select Directory", "", TempDoc))
{
TSearchRec sr;
int iAttributes = 0;
iAttributes |= faDirectory;
iAttributes |= faAnyFile;
AnsiString CurFile,FileExt;
TempDoc=IncludeTrailingBackslash(TempDoc);
if(FindFirst(TempDoc+"*", iAttributes, sr) == 0)
{
do
{
if((sr.Attr & faDirectory)==sr.Attr)
{
if(sr.Name.AnsiCompare(".") && sr.Name.AnsiCompare(".."))
{
AnsiString CurFile = sr.Name;
CurFile = TempDoc + CurFile;
ShowMessage(CurFile);
}
}
}while (FindNext(sr) == 0);
FindClose(sr);
}
}
}
不知道这个是不是你要的,找出目录下面的文件夹
cczlp 2006-05-24
  • 打赏
  • 举报
回复
大概没有

1,222

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder Windows SDK/API
社区管理员
  • Windows SDK/API社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧