CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
不看会后悔的Windows XP之经验谈 简单快捷DIY实用家庭影院
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  Delphi >  VCL组件开发及应用

关于FindFirst、FindNext

楼主xiaofeng_cxy(萧风)2002-07-01 10:51:49 在 Delphi / VCL组件开发及应用 提问

以下是找出所有当前目录的子目录的一段代码,可是不能成功,请各位大侠指教!  
  procedure   TForm1.Button2Click(Sender:   TObject);  
  var  
      SearchRec:TSearchRec;  
  begin  
      if   FindFirst(DirectoryListBox1.Directory+'*.*',faDirectory,SearchRec)=0   then  
      begin  
          lbFiles.Items.Add(SearchRec.Name);  
           
          //以下的while循环不能执行?????  
          while   FindNext(SearchRec)=0   do      
          begin  
              lbFiles.Items.Add(SearchRec.Name);  
              ShowMessage(SearchRec.Name);  
          end;  
      end;  
      FindClose(SearchRec);  
  end;  
   
  请帮忙,我在线恭候!! 问题点数:20、回复次数:6Top

1 楼netlib(河外孤星)回复于 2002-07-01 10:54:46 得分 0

这是delphi的例子,你看看  
  procedure   TForm1.Button1Click(Sender:   TObject);  
   
  var  
      sr:   TSearchRec;  
      FileAttrs:   Integer;  
  begin  
      StringGrid1.RowCount   :=   1;  
      if   CheckBox1.Checked   then  
          FileAttrs   :=   faReadOnly  
      else  
          FileAttrs   :=   0;  
      if   CheckBox2.Checked   then  
          FileAttrs   :=   FileAttrs   +   faHidden;  
      if   CheckBox3.Checked   then  
          FileAttrs   :=   FileAttrs   +   faSysFile;  
      if   CheckBox4.Checked   then  
          FileAttrs   :=   FileAttrs   +   faVolumeID;  
      if   CheckBox5.Checked   then  
   
          FileAttrs   :=   FileAttrs   +   faDirectory;  
      if   CheckBox6.Checked   then  
          FileAttrs   :=   FileAttrs   +   faArchive;  
      if   CheckBox7.Checked   then  
   
          FileAttrs   :=   FileAttrs   +   faAnyFile;  
   
      with   StringGrid1   do  
      begin  
          RowCount   :=   0;  
   
          if   FindFirst(Edit1.Text,   FileAttrs,   sr)   =   0   then  
   
          begin  
              repeat  
                  if   (sr.Attr   and   FileAttrs)   =   sr.Attr   then  
                  begin  
                  RowCount   :=   RowCount   +   1;  
                  Cells[1,RowCount-1]   :=   sr.Name;  
                  Cells[2,RowCount-1]   :=   IntToStr(sr.Size);  
                  end;  
              until   FindNext(sr)   <>   0;  
              FindClose(sr);  
          end;  
      end;  
  end;Top

2 楼xiaofeng_cxy(萧风)回复于 2002-07-01 11:16:25 得分 0

你这说明的只是查找文件啊,我想找出当前目录下所有的子目录啊!!  
  Top

3 楼PoolD(池龙)回复于 2002-07-01 11:27:37 得分 20

用来查找子目录的函数,结果返回到一个TStringList中。  
   
  function   DirsInDir(Dir:   string;   Mask:   string;   var   DirNames:   TStringList;   FullDirName:   boolean   =   false):   boolean;  
  //Find   Directory   in   Dir  
  var  
      FindHandle:   THandle;  
      FindData:   Win32_Find_Data;  
      procedure   Add;  
      var  
          vDirName,   vFullDirName:   string;  
      begin  
  {         if   FileExists(Dir   +   FindData.cFileName)  
              and   (FindData.dwFileAttributes   and   FILE_ATTRIBUTE_DIRECTORY   =   0)  
                  and   (FindData.dwFileAttributes   and   FILE_ATTRIBUTE_OFFLINE   =   0)  
                      and   (FindData.dwFileAttributes   and   FILE_ATTRIBUTE_TEMPORARY   =   0)  
                          then}  
          vDirName   :=   FindData.cFileName;  
          vFullDirName   :=   Dir   +   vDirName;  
          if   not((vDirName   =   '.')   or   (vDirName   =   '..')   or   (Pos(':\RECYCLED',   Uppercase(vFullDirName))   >   0))  
              and   DirectoryExists(vFullDirName)   then   begin  
                  if   FullDirName   then  
                      DirNames.Add(vFullDirName)  
                  else  
                      DirNames.Add(vDirName);  
          end;  
      end;  
  begin  
      Result   :=   false;  
      Dir   :=   IncludeTrailingBackSlash(Dir);  
      DirNames.Clear;  
      FindHandle   :=   FindFirstFile(PChar(Dir   +   Mask),   FindData);  
      if   FindHandle   <>   0   then   begin  
          Add;  
          While   FindNextFile(FindHandle,   FindData)   do   Add;  
          Windows.FindClose(FindHandle);  
          if   DirNames.Count   >   0   then   Result   :=   true;  
      end;  
  end;  
  Top

4 楼xiaofeng_cxy(萧风)回复于 2002-07-01 11:58:22 得分 0

我只想知道我的代码中到底有什么问题呢?  
  能不能不要答非所问啊?  
  Top

5 楼xiaofeng_cxy(萧风)回复于 2002-07-01 15:03:25 得分 0

我知道了,是因为没有在目录字符串后加'\'  
  谢谢各位Top

相关问题

  • FindClose是用来终结FindFirst还是FindNext?
  • findfirst,findnext,findclose是不是ANSI C函数
  • findfirst(), findnext() 在哪个.h 文件定义?
  • 用findfirst 和 findnext怎么样区分文件和目录?
  • 在win2000用FindFirst,FindNext等出现的问题?
  • FindFirst()和FindNext的用法!|!高手过来帮一下!
  • 有FindFirst、FindNext遍历文件函数的Unicode版本吗?
  • 如何区别用Findfirst和Findnext查找到的是文件还是文件夹????
  • FindFirst(...,faAnyFile,sr)和FindNext(sr)为什么只返回了目录,不返回文件?
  • 在dos.h找不到_find_t结构,_dos_findfirst,_dos_findnext,_dos_getfileattr等同类函数.

关键词

  • fileattrs
  • finddata
  • vfulldirname
  • vdirname
  • findhandle
  • searchrec
  • findfirst
  • findnext
  • 子目录
  • rowcount

得分解答快速导航

  • 帖主:xiaofeng_cxy
  • PoolD

相关链接

  • Delphi类图书
  • Delphi类源码下载
  • Delphi控件下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo