如果有子目录在函数最后加个判断递归查询就可以了
procedure findall(disk,path: String; var fileresult: Tstrings);
var
fpath: String;
fs: TsearchRec;
begin
fpath:=disk+path+'\*.*';
if findfirst(fpath,faAnyFile,fs)=0 then
begin
if (fs.Name<>'.')and(fs.Name<>'..') then
if (fs.Attr and faDirectory)=faDirectory then
findall(disk,path+'\'+fs.Name,fileresult)
else
fileresult.add(disk+strpas(strupper(pchar(path)))+'\'+strpas(
strupper(pchar(fs.Name)))+'('+inttostr(fs.Size)+')');
while findnext(fs)=0 do
begin
if (fs.Name<>'.')and(fs.Name<>'..') then
if (fs.Attr and faDirectory)=faDirectory then
findall(disk,path+'\'+fs.Name,fileresult)
else
fileresult.add(disk+strpas(strupper(pchar(path)))+'\'+str
pas(strupper(pchar(fs.Name)))+'('+inttostr(fs.Size)+')');
end;
end;
findclose(fs);
end;
再来一个:
function GetFileList(path:string;Attr:integer;FileList:TStringList):boolean;
var
SearchRec: TSearchRec;
i:integer;
begin
i:=FindFirst(path, attr, SearchRec);
if i <> 0 then
begin
result := false;
exit;
end;
while i = 0 do
begin
FileList.Add(SearchRec.Name);
i:=FindNext(SearchRec);
end;
FindClose(SearchRec);
result := true;
end;
使用范例:
var
FileList:Tstringlist;
begin
filelist := Tstringlist.Create;
getfilelist(ExtractFilePath(Application.ExeName)+'*.*',faAnyFile,filelist
filelist.free;
//得到某一文件夹下所有文件列表的函数 /////////////////////////////////////
// getFileListFromDir('c:\windows',false,fileList);
//c:\windows or c: ture|false
procedure getFileListFromDir(dir:string;includeSubDir:boolean;var fileList:TStringList);
var
sr:TSearchRec;
path:String;
begin
path:=dir+'\'; //c:\windows\
dir:=dir+'\*.*'; //c:\windows\*.*
if findFirst(dir,faanyfile,sr)=0 then
repeat
begin
if (sr.Name<>'.') and (sr.Name<>'..') and ((fileGetAttr(path+sr.Name) and faDirectory)<>faDirectory) then
fileList.Add(path+sr.Name);
if (includeSubDir) and (sr.Name<>'.') and (sr.Name<>'..') and ((fileGetAttr(path+sr.Name) and faDirectory)=faDirectory) then
getFileListFromDir(path+sr.Name,includeSubDir,fileList);
end;
until findNext(sr)<>0;
findClose(sr);
end;
procedure FileSearch(PathName:string);
var
F : TSearchRec;
Found : Boolean;
begin
ChDir(PathName);
Found := (FindFirst('*.*', faAnyFile, F) = 0);
while Found do
begin
if (F.Name = '.') or (F.Name = '..') then
begin
Found := (FindNext(F) = 0);
Continue;
end;
if (F.Attr and faDirectory)>0 then
begin
Application.ProcessMessages;
FileSearch(F.Name);
end;
//插入你的代码,F.Name就是文件名,GetCurrentDir可以得到当前目录
Found := (FindNext(F) = 0);
end;
FindClose(F);
ChDir('..\');
end;