如何搜索硬盘中所有的*.doc文件和*.xls文件(100分)

  • 主题发起人 主题发起人 ww990
  • 开始时间 开始时间
W

ww990

Unregistered / Unconfirmed
GUEST, unregistred user!
如题,窗口中有一按钮和一文件列表框,点击按钮自动搜索,便将结果放入文件列表框中。
 
unit mainunit;

interface

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

type
TForm1 = class(TForm)
Memo1: TMemo;
Label1: TLabel;
Label2: TLabel;
ComboBox1: TComboBox;
Edit1: TEdit;
BitBtn1: TBitBtn;
BitBtn2: TBitBtn;
BitBtn3: TBitBtn;
Label3: TLabel;
procedure FormCreate(Sender: TObject);
procedure ComboBox1Change(Sender: TObject);
procedure Edit1Change(Sender: TObject);
procedure BitBtn1Click(Sender: TObject);
procedure BitBtn2Click(Sender: TObject);
procedure BitBtn3Click(Sender: TObject);
private
{ Private declarations }
public
function CurrentIsValidDir(SearchRec:TSearchRec):integer;
procedure RecurSearchFile(CurrentDir:string;SearchFileType:string;SearchResult:TStrings;var Number:integer);
{ Public declarations }
end;

var
Form1: TForm1;
TotalFileNumbers:Integer;
SearchFileType:String;
implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
TotalFileNumbers:=0;
SearchFileType:='*.txt';
end;

function TForm1.CurrentIsValidDir(SearchRec:TSearchRec):integer;
begin
if ((SearchRec.Attr <> 16) and
(SearchRec.Name<>'.') and
(SearchRec.Name<>'..')) then
Result:=0
else if ((SearchRec.Attr = 16) and
(SearchRec.Name<>'.') and
(SearchRec.Name<>'..')) then
Result:=1
else
Result:=2;
end;

Procedure TForm1.RecurSearchFile(CurrentDir:string;SearchFileType:string;SearchResult:TStrings;var Number:integer);
var
i:integer;
Subdir:TStringList;
SearchRec:TsearchRec;
begin
//第一次调用FindFirst和FindNext查找符合要求的文件
if (FindFirst(CurrentDir+SearchFileType, faAnyFile, SearchRec)=0) then
begin
repeat
if CurrentIsValidDir(SearchRec)=0 then
begin
Inc(Number);
Searchresult.Add(CurrentDir+SearchRec.Name);
end;
application.ProcessMessages ;
until (FindNext(SearchRec) <> 0);
end;
FindClose(SearchRec);

//以下是递归部分,查找各子目录。
Subdir:=TStringList.Create;
if (FindFirst(CurrentDir+'*.*', faDirectory, SearchRec)=0) then
begin
repeat
if CurrentIsValidDir(SearchRec)=1 then
begin
Subdir.Add(SearchRec.Name);
end;
application.ProcessMessages ;
until (FindNext(SearchRec) <> 0);
end;
FindClose(SearchRec);
for i:=0 to Subdir.Count-1 do
begin
RecurSearchfile(CurrentDir+Subdir.Strings+'/',SearchFileType,Searchresult,Number);
end;

//资源释放并返回结果。
Subdir.Free;
end;

procedure TForm1.ComboBox1Change(Sender: TObject);
begin
Case ComboBox1.ItemIndex of
0:SearchFileType:='*.txt';
1:SearchFileType:='*.bmp';
2:SearchFileType:='*.mp3';
3:SearchFileType:='*.*';
end;
end;

procedure TForm1.Edit1Change(Sender: TObject);
begin
if Edit1.Text='' then
BitBtn2.Enabled:=False
else
BitBtn2.Enabled:=True;
end;

procedure TForm1.BitBtn1Click(Sender: TObject);
var
SelectDir:string;
begin
if SelectDirectory(SelectDir, [sdAllowCreate, sdPerformCreate, sdPrompt],0) then
begin
if length(SelectDir) > 3 then
SelectDir:=SelectDir+'/';
Edit1.Text:=SelectDir;
end;
end;

procedure TForm1.BitBtn2Click(Sender: TObject);
begin
memo1.lines.Clear ;
TotalFileNumbers:=0;
RecurSearchFile(Edit1.Text,SearchFileType,memo1.lines, TotalFileNumbers);
Label3.Caption:='查找结果显示:'+'一共在当前目录下找到'+IntToStr(TotalFileNumbers)+'个'+SearchFileType+'类型的文件';
end;

procedure TForm1.BitBtn3Click(Sender: TObject);
begin
Close;
end;

end.
 
接受答案了.
 

Similar threads

回复
0
查看
1K
不得闲
S
回复
0
查看
695
SUNSTONE的Delphi笔记
S
S
回复
0
查看
783
SUNSTONE的Delphi笔记
S
后退
顶部