type TForm1=class(TForm) Memo1:TMemo; Button1:TButton; Button2:TButton; procedureButton1Click(Sender:TObject); procedureButton2Click(Sender:TObject); private { Private declarations } public { Public declarations } end;
var Form1:TForm1;
implementation usesMasks; {$R *.dfm}
//遍历目录及子目录 procedureGetFileListEx(FilePath,ExtMask:string;FileList:TStrings;SubDirectory:Boolean=True); functionMatch(FileName:string;MaskList:TStrings):boolean; var i:Integer; begin Result:=False; fori:=0toMaskList.Count-1do begin ifMatchesMask(FileName,MaskList[i])then begin Result:=True; break; end; end; end; var FileRec:TSearchrec; MaskList:TStringList; begin ifDirectoryExists(FilePath)then begin ifFilePath[Length(FilePath)]<>‘\‘thenFilePath:=FilePath+‘\‘; ifFindFirst(FilePath+‘*.*‘,faAnyfile,FileRec)=0then begin MaskList:=TStringList.Create; try ExtractStrings([‘;‘],[],PChar(ExtMask),MaskList); FileList.BeginUpdate; repeat if((FileRec.AttrandfaDirectory)<>0)andSubDirectorythen begin if(FileRec.Name<>‘.‘)and(FileRec.Name<>‘..‘)then GetFileListEx(FilePath+FileRec.Name+‘\‘,ExtMask,FileList); end else begin ifMatch(FilePath+FileRec.Name,MaskList)then FileList.Add({FilePath +}FileRec.Name); end; untilFindNext(FileRec)<>0; FileList.EndUpdate; finally MaskList.Free; end; end; FindClose(FileRec); end; end;
procedureTForm1.Button1Click(Sender:TObject); begin Memo1.Lines.Clear; GetFileListEx(‘Z:\‘,‘*.*‘,Memo1.Lines,False); Caption:=IntToStr(Memo1.Lines.count); end;
procedureTForm1.Button2Click(Sender:TObject); begin