给你一个例子:
var
Flist:Tstringlist;
i:integer;
begin
FList:= TStringList.Create;
SearchFile('C:\abc'+'\*.*', FList, faDirectory, True);
FileLV.items.BeginUpdate;
FileLV.items.clear;
For I:=0 To FList.Count -1 Do
With FileLV.Items.Add Do
Caption:= FLIst.Strings[I];
FileLV.items.EndUpdate;
FList.Free;
End;
Procedure CustomSearchFile(SearchPath: String; List: TStringList;
ExcludeAttr: Integer; InDir: Boolean);
Var
Index: Integer;
NextDir: Boolean;
FindData: TWin32FindData;
FindHandle: LongWord;
FileName: TFileName;
Begin
IF Copy(SearchPath, Length(SearchPath), 1)<>'\' Then SearchPath:= SearchPath+ '\';
FindHandle:= FindFirstFile(PChar(SearchPath+ '*.*'), FindData);
NextDir:= False;
Index:= List.Count;
While True Do Begin
IF NextDir And InDir Then Begin
FindHandle:=INVALID_HANDLE_VALUE;
While Index<List.Count Do Begin
IF Not FileExists(List.Strings[Index]) Then Inc(Index)
Else
Begin
SearchPath:= List.Strings[Index]+'\';
FindHandle:= FindFirstFile(pChar(SearchPath+ '*.*'), FindData);
Break;
End;
End;
End;
IF FindHandle= INVALID_HANDLE_VALUE Then Break;
FileName:= FindData.cFileName;
IF (FindData.dwFileAttributes And ExcludeAttr)=0 Then
IF FileName<>'.' Then
IF FileName<>'..' Then
List.Add(SearchPath+ FileName);
IF Not FindNextFile(FindHandle, FindData) Then Begin
windows.FindClose(FindHandle);
IF InDir Then NextDir:= True Else Break;
End;
End;
End;
Procedure SearchDirectory(Directory: String; DList: TStringList;
ExcludeAttr: Integer; InDir: Boolean);
Var
Index: Integer;
Begin
IF ExcludeAttr=-1 Then ExcludeAttr:= faSysFile OR faHidden;
IF (ExcludeAttr And faDirectory)= faDirectory Then
ExcludeAttr:= ExcludeAttr Xor faDirectory;
Index:= DList.Count;
CustomSearchFile(Directory, DList, ExcludeAttr, InDir);
For Index:=DList.Count-1 DownTo Index Do
IF FileExists(DList.Strings[Index]) Then DList.Delete(Index);
End;
Procedure SearchFile(FileName: TFileName; FList: TStringList;
ExcludeAttr: Integer; InDir: Boolean);
Var
SPath, SFile: String;
Index, BufferIndex: Integer;
FindData: TWin32FindData;
FindHandle: LongWord;
Begin
SPath:= ExtractFilePath(FileName);
SFile:= ExtractFileName(FileName);
Index:= FList.Count;
BufferIndex:= Index;
IF ExcludeAttr=-1 Then ExcludeAttr:= faDirectory OR faSysFile OR faHidden;
While True Do Begin
IF InDir Then SearchDirectory(SPath, FList, ExcludeAttr, False);
FindHandle:= FindFirstFile(PChar(SPath+SFile), FindData);
While FindHandle<>INVALID_HANDLE_VALUE Do Begin
FileName:= FindData.cFileName;
IF (ExcludeAttr=0) OR ((FindData.dwFileAttributes And ExcludeAttr)=0) Then
IF FileExists(SPath+FileName) Then FList.Add(SPath+FileName);
IF Not FindNextFile(FindHandle, FindData) Then Begin
FindClose(FindHandle);
Break;
End;
End;
//
IF InDir Then
While Index<FList.Count Do Begin
IF FileExists(FList.Strings[Index]) Then Inc(Index)
Else Begin
SPath:= FList.Strings[Index]+'\';
Inc(Index);
Break;
End;
End;
IF Index>=Flist.Count Then Break;
End;
IF (ExcludeAttr And faDirectory)=faDirectory Then
For Index:=FList.Count-1 DownTo BufferIndex Do
IF Not FileExists(FList.Strings[Index]) Then FList.Delete(Index);
end;
给你一个例子:
var
Flist:Tstringlist;
i:integer;
begin
FList:= TStringList.Create;
SearchFile('C:\abc'+'\*.*', FList, faDirectory, True);
FileLV.items.BeginUpdate;
FileLV.items.clear;
For I:=0 To FList.Count -1 Do
With FileLV.Items.Add Do
Caption:= FLIst.Strings[I];
FileLV.items.EndUpdate;
FList.Free;
End;
Procedure CustomSearchFile(SearchPath: String; List: TStringList;
ExcludeAttr: Integer; InDir: Boolean);
Var
Index: Integer;
NextDir: Boolean;
FindData: TWin32FindData;
FindHandle: LongWord;
FileName: TFileName;
Begin
IF Copy(SearchPath, Length(SearchPath), 1)<>'\' Then SearchPath:= SearchPath+ '\';
FindHandle:= FindFirstFile(PChar(SearchPath+ '*.*'), FindData);
NextDir:= False;
Index:= List.Count;
While True Do Begin
IF NextDir And InDir Then Begin
FindHandle:=INVALID_HANDLE_VALUE;
While Index<List.Count Do Begin
IF Not FileExists(List.Strings[Index]) Then Inc(Index)
Else
Begin
SearchPath:= List.Strings[Index]+'\';
FindHandle:= FindFirstFile(pChar(SearchPath+ '*.*'), FindData);
Break;
End;
End;
End;
IF FindHandle= INVALID_HANDLE_VALUE Then Break;
FileName:= FindData.cFileName;
IF (FindData.dwFileAttributes And ExcludeAttr)=0 Then
IF FileName<>'.' Then
IF FileName<>'..' Then
List.Add(SearchPath+ FileName);
IF Not FindNextFile(FindHandle, FindData) Then Begin
windows.FindClose(FindHandle);
IF InDir Then NextDir:= True Else Break;
End;
End;
End;
Procedure SearchDirectory(Directory: String; DList: TStringList;
ExcludeAttr: Integer; InDir: Boolean);
Var
Index: Integer;
Begin
IF ExcludeAttr=-1 Then ExcludeAttr:= faSysFile OR faHidden;
IF (ExcludeAttr And faDirectory)= faDirectory Then
ExcludeAttr:= ExcludeAttr Xor faDirectory;
Index:= DList.Count;
CustomSearchFile(Directory, DList, ExcludeAttr, InDir);
For Index:=DList.Count-1 DownTo Index Do
IF FileExists(DList.Strings[Index]) Then DList.Delete(Index);
End;
Procedure SearchFile(FileName: TFileName; FList: TStringList;
ExcludeAttr: Integer; InDir: Boolean);
Var
SPath, SFile: String;
Index, BufferIndex: Integer;
FindData: TWin32FindData;
FindHandle: LongWord;
Begin
SPath:= ExtractFilePath(FileName);
SFile:= ExtractFileName(FileName);
Index:= FList.Count;
BufferIndex:= Index;
IF ExcludeAttr=-1 Then ExcludeAttr:= faDirectory OR faSysFile OR faHidden;
While True Do Begin
IF InDir Then SearchDirectory(SPath, FList, ExcludeAttr, False);
FindHandle:= FindFirstFile(PChar(SPath+SFile), FindData);
While FindHandle<>INVALID_HANDLE_VALUE Do Begin
FileName:= FindData.cFileName;
IF (ExcludeAttr=0) OR ((FindData.dwFileAttributes And ExcludeAttr)=0) Then
IF FileExists(SPath+FileName) Then FList.Add(SPath+FileName);
IF Not FindNextFile(FindHandle, FindData) Then Begin
FindClose(FindHandle);
Break;
End;
End;
//
IF InDir Then
While Index<FList.Count Do Begin
IF FileExists(FList.Strings[Index]) Then Inc(Index)
Else Begin
SPath:= FList.Strings[Index]+'\';
Inc(Index);
Break;
End;
End;
IF Index>=Flist.Count Then Break;
End;
IF (ExcludeAttr And faDirectory)=faDirectory Then
For Index:=FList.Count-1 DownTo BufferIndex Do
IF Not FileExists(FList.Strings[Index]) Then FList.Delete(Index);
end;
private
{ Private declarations }
public
FFileName:String; //查找的文件名
function GetDirectoryName(Dir:String):String;
Procedure FindFiles(APath:String);
{ Public declarations }
end;
var
filefindfrm: Tfilefindfrm;
implementation
{$R *.dfm}
function Tfilefindfrm.GetDirectoryName(Dir:String):String; //设置DIR
begin
if Dir[LengTh(Dir)]<>'\' then
Result:=Dir+'\'
else
Result:=Dir;
end;
procedure TfilefindFrm.FindFiles(APath:String); //查找主函数
var
FSearchRec,DsearchRec:TSearchRec;
FindResult:integer;
function IsDirNotation(ADirName:String):Boolean;
begin
Result:=(ADirName='.') or(ADirName='..');
End;
begin
Apath:=GetDirectoryName(APath);
FIndResult:=FindFirst(APath+FFileName,faAnyFile+faHidden+faSysFile+FaReadonly,FsearchRec);
Try
while FindResult=0 do
begin
lbFiles.Items.Add(LowerCase(APath+FSearchRec.Name)); //把查找到的文件列出来
FindResult:=FindNext(FSearchRec);
end;
FindReSult:=FindFirst(APath+'*.*',faDirectory,DSearchRec);
while FindResult=0 do
begin
if ((DSearchRec.Attr and faDirectory)=faDirectory) and not ISDirNotation(DSEarchRec.Name) then
FindFiles(APath+DsearchRec.Name);
FindResult:=FindNext(DsearchREc);
end;
Finally
FindClose(FSearchRec);
end;
end;