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 xe 如何使用自带皮肤

Delphi dbexpress的upwherekeyonly的使用注意事项

Delphi system.messaging.pas例子

Delphi tstreamwriter快速写入文件

windows 消息

Delphi imagelist-图片(bmp)按名称而不按索引

Delphi uac相关代码

Delphi idwhois1 简单的用法

Delphi 将base64字符串转化为jpeg图片

Delphi win7,win2008,win2003,winxp 屏蔽ctrl+alt+del

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



打赏

取消

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

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

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

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

评论

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