delphi 如何遍历整个硬盘的各个目录,就像windows的查找功能那样


本文整理自网络,侵删。

 

注:输入c:\** 即可遍历C盘

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    Edit1: TEdit;
    procedure Button1Click(Sender: TObject);
private
    { Private declarations }
public
    { Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}
procedure   search(dir:string);  
var  
      targetpath:string;{目标路径名}  
      sr:TsearchRec;  
begin  
      {第一阶段:找出初始dir目录下的所有文件,  
      其中dir变量值由edit1的Text属性确定}  

      targetpath:=extractfilepath(dir);{分解出目标路径名}  
      if   findfirst(dir,faanyfile,sr)=0   then  
      repeat  
        if((sr.name<>'.')and(sr.name<>'..'){排除父目录和本目录两个假文件}  
        and((filegetattr(targetpath+sr.name)and   fadirectory)<>fadirectory)){只取文件}  
        then
            form1.memo1.Lines.Add(targetpath+sr.name);{在memo中添加找到的文件}  
      until   findnext(sr)<>0;  
   
      if   findfirst(dir,faanyfile,sr)=0   then  
      repeat  
        if((sr.name<>'.')and(sr.name<>'..')){排除父目录和本目录两个假文件}  
        and((filegetattr(targetpath+sr.name)and   fadirectory)=fadirectory){排除文件}
        then
            search(targetpath+sr.name+'\*.*');{递归调用}
      until   findnext(sr)<>0;  
end;  
   


procedure TForm1.Button1Click(Sender: TObject);
begin
memo1.Clear;{清除数据表memo字段内容}  
search(Edit1.Text);{调用Search()函数}
MessageDlg('文件搜索完毕!',mtInformation,[mbOk],0);{结束提示}

end;

end.

相关阅读 >>

Delphi 中的颜色常量及效果图

Delphi2009 使用 png 图片

Delphi中判断字符串是否为数字

Delphi中使用goo.gl(google的缩短url服务)api

Delphi实现票据精确打印

Delphi memo中禁止汉字

Delphi 全局变量 hinstance 到底是在什么时候赋值的?

Delphi unigui执行程序部署有3种形式

Delphixe8中获取apk的签名信息

Delphi upload 上传文件

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...