CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
山寨机中的战斗机! 程序优化工程师到底对IT界有没有贡献
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  Delphi >  Windows SDK/API

如何获得目录下的所有文件名以及子文件夹名?

楼主realhero(虚心学习,天天向上)2003-11-01 21:20:47 在 Delphi / Windows SDK/API 提问

如何获得目录下的所有文件名以及子文件夹名? 问题点数:0、回复次数:9Top

1 楼vagerent(上午的绝缘杯)回复于 2003-11-01 21:24:48 得分 0

这种资料很多,你最好自己找找,也可能受到新的启发。授人以鱼不如授人以渔。Top

2 楼maozefa(阿发伯)回复于 2003-11-01 21:55:22 得分 0

procedure   TForm1.Button1Click(Sender:   TObject);  
  var  
      SearchRec:   TSearchRec;  
      i:   Integer;  
  begin  
      ListBox1.Clear;  
        i   :=   FindFirst('D:\*.*',   faAnyFile,   SearchRec);  
        while   i   =   0   do  
        begin  
            ListBox1.Items.Add(SearchRec.Name);  
            i   :=   FindNext(SearchRec);  
        end;  
        FindClose(SearchRec);  
  end;  
  Top

3 楼realhero(虚心学习,天天向上)回复于 2003-11-02 20:53:01 得分 0

那个while循环i=0这个条件什么意思?Top

4 楼vagerent(上午的绝缘杯)回复于 2003-11-03 13:51:08 得分 0

FindNext(SearchRec);返回值如果为0是指找到要找的文件  
   
  还有,上面的代码实现不了你说的功能,还是自己找找吧  
  到www.51delphi.com或http://skdweb.sdust.edu.cn/cree/amsite/看看Top

5 楼litangel(还是朋友)回复于 2003-11-03 14:27:41 得分 0

i   :=   FindFirst('D:\*.*',   faAnyFile,   SearchRec);  
  i是findfirst的返回值,你在delphi里看一下   FindFirst  
  的帮助就知道了。Top

6 楼guxizhw(失落的彩虹)回复于 2003-11-03 14:37:22 得分 0

对,2楼的说的不能实现子目录里面的查找,不过你可以判断一下,一旦找到的是子目录,你就递归调用一下那个procedure,继续找就行了,只要……你的内存足够大Top

7 楼yanghai0437(流浪者)回复于 2003-11-03 15:55:14 得分 0

在Delphi中实现对目录拷贝、删除和搬移的操作  
  (   阅读次数:4)      
  笔者在工作中遇到了需要对目录进行拷贝、删除和搬移的需求,Delphi本身提供了一些目录操作函数,但只是针对空目录而言,对目录下带有子目录的情况,更是无能为力。利用Win32   API函数和结构,以及递归的程序设计思想,笔者实现了对任意目录进行拷贝、删除和搬移的功能(分别相当于DOS中的XCopy、DelTree和Move命令)。以下分别给出了实现代码:    
    1、拷贝目录    
    为了能拷贝目录下带有子目录的情况,先定义一个辅助的拷贝函数,它是递归执行的,直到把目录下的所有文件和子目录都拷贝完。    
    1.1拷贝目录的递归辅助函数:DoCopyDir    
  function   DoCopyDir(sDirName:String;  
  sToDirName:String):Boolean;  
  var  
  hFindFile:Cardinal;  
  t,tfile:String;  
  sCurDir:String[255];  
  FindFileData:WIN32_FIND_DATA;  
  begin  
  //先保存当前目录  
  sCurDir:=GetCurrentDir;  
  ChDir(sDirName);  
  hFindFile:=FindFirstFile('*.*',FindFileData);  
  if   hFindFile<   >INVALID_HANDLE_VALUE   then  
  begin  
  if   not   DirectoryExists(sToDirName)   then  
  ForceDirectories(sToDirName);  
  repeat  
  tfile:=FindFileData.cFileName;  
  if   (tfile='.')   or   (tfile='..')   then  
  Continue;  
  if   FindFileData.dwFileAttributes=  
  FILE_ATTRIBUTE_DIRECTORY   then  
  begin  
  t:=sToDirName+'\'+tfile;  
  if   not   DirectoryExists(t)   then  
  ForceDirectories(t);  
  if   sDirName[Length(sDirName)]<   >'\'   then  
  DoCopyDir(sDirName+'\'+tfile,t)  
  else  
  DoCopyDir(sDirName+tfile,sToDirName+tfile);  
  end  
  else  
  begin  
  t:=sToDirName+'\'+tFile;  
  CopyFile(PChar(tfile),PChar(t),True);  
  end;  
  until   FindNextFile(hFindFile,FindFileData)=false;  
  FindClose(hFindFile);  
  end  
  else  
  begin  
  ChDir(sCurDir);  
  result:=false;  
  exit;  
  end;  
  //回到原来的目录下  
  ChDir(sCurDir);  
  result:=true;  
  end;  
  ----   1.2拷贝目录的函数:CopyDir    
  function   CopyDir(sDirName:String;  
  sToDirName:string):Boolean;  
  begin  
  if   Length(sDirName)<   =0   then  
  exit;  
  //拷贝...  
  Result:=DoCopyDir(sDirName,sToDirName);  
  end;  
    2、删除目录    
    删除目录与拷贝目录很类似,但为了能删除位于根目录下的一个空目录,需要在辅助函数中设置一个标志变量,即:如果删除的是空目录,则置bEmptyDir为True,这一句已经用深色框表示了。    
    2.1删除目录的递归辅助函数:DoRemoveDir    
  function   DoRemoveDir(sDirName:String):Boolean;  
  var  
  hFindFile:Cardinal;  
  tfile:String;  
  sCurDir:String;  
  bEmptyDir:Boolean;  
  FindFileData:WIN32_FIND_DATA;  
  begin  
  //如果删除的是空目录,则置bEmptyDir为True  
  //初始时,bEmptyDir为True  
  bEmptyDir:=True;  
  //先保存当前目录  
  sCurDir:=GetCurrentDir;  
  SetLength(sCurDir,Length(sCurDir));  
  ChDir(sDirName);  
  hFindFile:=FindFirstFile('*.*',FindFileData);  
  if   hFindFile<   >INVALID_HANDLE_VALUE   then  
  begin  
  repeat  
  tfile:=FindFileData.cFileName;  
  if   (tfile='.')   or   (tfile='..')   then  
  begin  
  bEmptyDir:=bEmptyDir   and   True;  
  Continue;  
  end;  
  //不是空目录,置bEmptyDir为False  
  bEmptyDir:=False;  
  if   FindFileData.dwFileAttributes=  
  FILE_ATTRIBUTE_DIRECTORY   then  
  begin  
  if   sDirName[Length(sDirName)]<   >'\'   then  
  DoRemoveDir(sDirName+'\'+tfile)  
  else  
  DoRemoveDir(sDirName+tfile);  
  if   not   RemoveDirectory(PChar(tfile))   then  
  result:=false  
  else  
  result:=true;  
  end  
  else  
  begin  
  if   not   DeleteFile(PChar(tfile))   then  
  result:=false  
  else  
  result:=true;  
  end;  
  until   FindNextFile(hFindFile,FindFileData)=false;  
  FindClose(hFindFile);  
  end  
  else  
  begin  
  ChDir(sCurDir);  
  result:=false;  
  exit;  
  end;  
  //如果是空目录,则删除该空目录  
  if   bEmptyDir   then  
  begin  
  //返回上一级目录  
  ChDir('..');  
  //删除空目录  
  RemoveDirectory(PChar(sDirName));  
  end;    
   
  //回到原来的目录下  
  ChDir(sCurDir);  
  result:=true;  
  end;  
    2.2删除目录的函数:DeleteDir    
  function   DeleteDir(sDirName:String):Boolean;  
  begin  
  if   Length(sDirName)<   =0   then  
  exit;  
  //删除...  
  Result:=DoRemoveDir(sDirName)   and   RemoveDir(sDirName);  
  end;  
    3、移动目录    
    有了拷贝目录和删除目录的函数,移动目录就变得很简单,只需顺序调用前两个函数即可:    
  function   MoveDir(sDirName:String;  
  sToDirName:string):Boolean;  
  begin  
  if   CopyDir(sDirName,sToDirName)   then  
  if   RemoveDir(sDirName)   then  
  result:=True  
  else  
  result:=false;  
  end;  
  Top

8 楼barely(一哥)回复于 2003-11-03 15:56:41 得分 0

找出所有的文件与目录:  
   
  procedure   TFTPMain.LocalUpdateListing;  
  var  
          F:   TWin32FindData;  
          Enum:   Hwnd;  
          R:   Bool;  
          FileName:   string;  
          FileSize:   Int64;  
          Magic:integer;  
          item1:TListItem;  
  begin  
          Pointer(Enum)   :=   nil;  
          with   Lview1   do  
          try  
          Clear;  
          Update;  
          Enum   :=   FindFirstFile(PChar(FLocalPath   +   '*.*'),   F);  
          R   :=   Pointer(Enum)   <>   nil;  
          Magic:=0;  
          while   R   do  
          begin  
                  FileName   :=   F.cFileName;  
                  if   filename   <>'.'   then  
                  begin  
                          item1:=Lview1.Items.Add();  
                          item1.SubItems.Add('');  
                          item1.SubItems.Add('');  
                          item1.SubItems.Add('');  
                          LvAdd(Filename,0,0,item1);  
                          FileSize   :=   (F.nFileSizeHigh   shl   32)   or   (F.nFileSizeLow);  
                          if   F.dwFileAttributes   and   faDirectory   =   faDirectory   then  
                          begin  
                                  item1.ImageIndex:=1;  
                                  item1.SubItems.Strings[0]:='目录';//文件夹  
                                  item1.SubItems.Strings[1]:=   '0';  
                          end  
                          else  
                          begin  
                                  item1.ImageIndex:=0;  
                                  item1.SubItems.Strings[0]:='';//文件夹  
                                  item1.SubItems.Strings[1]:=   FileSizeToString(FileSize);  
                                  item1.SubItems.Strings[2]:=  
                                  GetFileTypeDescription(FRemotePath   +   FileName,   True);  
                          end;  
                  end;  
                  R   :=   FindNextFile(Enum,   F);  
          end;  
          finally  
                  windows.FindClose(Enum);  
                  RemoteSort:=False;  
          end;  
  end;  
   
  function   TFTPMain.GetFileTypeDescription(const   Name:   string;   UseAttr:   Boolean):   string;  
  var  
          Info:   TSHFileInfo;  
          Flags:   Cardinal;  
  begin  
          FillChar(Info,   SizeOf(Info),   0);  
          Flags   :=   SHGFI_TYPENAME;  
          if   UseAttr   then   Flags   :=   Flags   or   SHGFI_USEFILEATTRIBUTES;  
          SHGetFileInfo(PChar(Name),   0,   Info,   SizeOf(Info),   Flags);  
                  Result   :=   Info.szTypeName;  
  end;  
   
  function   TFTPMain.FileSizeToString(const   Size:   Int64):   string;  
  var  
          S:   Integer;  
  begin  
          if   Size   <   1024   then  
                  Result   :=   IntToStr(Size)   +   '   Bytes';  
          S   :=   Size   div   1024;  
          if   S   =   0   then   S   :=   1;  
          if   S   <   1024   then  
                  Result   :=   IntToStr(S)   +   '   KB'  
          else  
          begin  
                  S   :=   S   div   1024;  
                  if   S   =   0   then   S   :=   1;  
                  Result   :=   IntToStr(S)   +   '   MB';  
          end;  
     
  end;  
  Top

9 楼maozefa(阿发伯)回复于 2003-11-03 17:57:07 得分 0

楼主的要求是“如何获得目录下的所有文件名以及子文件夹名”,并没有说包含子文件夹里的...,因此我上面的例子才是正解,要包含子文件夹里的。修改一下,递归就可以了。不过这样意义不大,即使要做一个文件列表对话框,也是点击子目录后,再搜索该子目录下的内容。Top

相关问题

  • 怎样才能获得一个目录下所有子文件夹名和文件名
  • 怎样获得某文件夹下的文件名啊?
  • 怎样获得指定文件夹下的全部文件名?
  • 怎么获得文件夹下面的文件名呢?
  • 如何获得特定目录(文件夹)下的所有文件,我要把它们加入treeview,文件名作为节点名。
  • 用C# 如何获得特定目录(文件夹)下的所有文件,我要把它们加入treeview,文件名作为节点名。
  • 怎样获得一个文件夹下的所有文件名包括文件夹名?
  • 怎么样连续的获得同一文件夹中的文件名!
  • 怎样用c语言获得一个文件夹中的所有文件名?
  • 如何获得特定文件夹下的所有文件的文件名?急!

关键词

  • 函数
  • 文件夹
  • 拷贝
  • sdirname
  • tfile
  • stodirname
  • scurdir
  • 目录
  • findfiledata
  • 空目录

得分解答快速导航

  • 帖主:realhero

相关链接

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

广告也精彩

反馈

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