怎么获得一个文件夹内(包括子文件夹)的所有文件啊?(30分)

  • 主题发起人 主题发起人 nofault
  • 开始时间 开始时间
N

nofault

Unregistered / Unconfirmed
GUEST, unregistred user!
昨天问过,用FindFirst(FindNext,FindClose)可以搜索一个文件夹内的文件,但是怎么获得所有的文件啊,而且包括子文件夹?
 
下面是:

定义全局变量
SearchFileList:TStrings;

er
SearchFileList:=TStringList.Create;

//
Procedure TForm_CopyDisk.GetFileName(StrPath:string);
var
FileAttrs:integer;
SearchResult: TSearchRec;
Str,SearchPath:String;
ErrorInfo: word;
begin
ErrorInfo := SetErrorMode(SEM_FAILCRITICALERRORS);
try
FileAttrs :=faAnyFile;
SearchPath:=StrPath+'*.*';
if FindFirst(SearchPath, FileAttrs, SearchResult) = 0 then
begin
if SearchResult.Attr<>faDirectory then
begin
Str:=StrPath+StrUpper(pchar(SearchResult.name));
SearchFileList.Add(Str);
end;

while FindNext(SearchResult) = 0 do
begin
if SearchResult.Attr<>faDirectory then
begin
Str:=StrPath+StrUpper(pchar(SearchResult.name));
SearchFileList.Add(Str);
end;
end;
FindClose(SearchResult);
end;
finally
SetErrorMode(ErrorInfo);
end;
end;
 
procedure FindAllFiles(APath:string);
var
re:integer;
se:TSearchRec;
begin
re:=FindFirst(Apath+'*.*',faanyfile,se);
while re=0 do begin
if (se.name<>'.') and (se.name<>'.') then
if (se.attr and fadirectory)=fadirectory then
FindAllFiles(APath+se.name) //这里是文件夹
else else se.name........//这里是文件
end;
end;

先试试八,不行再找我,代码不一定对,你多试试
 
更正: While re=0 do begin
...
re:=FindNext(se);
end;
 
上面的写错了:

应该是现面的递规函数
Procedure TForm_CopyDisk.GetFileName(StrPath:string);
var
FileAttrs:integer;
SearchResult: TSearchRec;
Str,SearchPath:String;
ErrorInfo: word;
begin
ErrorInfo := SetErrorMode(SEM_FAILCRITICALERRORS);
try
FileAttrs :=faAnyFile;
SearchPath:=StrPath+'*.*';
if FindFirst(SearchPath, FileAttrs, SearchResult) = 0 then
begin
if SearchResult.Attr<>faDirectory then
begin
Str:=StrPath+StrUpper(pchar(SearchResult.name));
SearchFileList.Add(Str);
end;
if (Sr.Attr=faDirectory) and (sr.Name<>'.')then
begin
SearchPath:=StrPath+sr.name;
GetFileName(SearchPath);
end;
while FindNext(SearchResult) = 0 do
begin
if SearchResult.Attr<>faDirectory then
begin
Str:=StrPath+StrUpper(pchar(SearchResult.name));
SearchFileList.Add(Str);
end;
if (Sr.Attr=faDirectory) and (sr.Name<>'.')then
begin
SearchPath:=StrPath+sr.name;
GetFileName(SearchPath);
end;
end;
FindClose(SearchResult);
end;
finally
SetErrorMode(ErrorInfo);
end;
end;
 
记得最后:FindClose(se);
 
ADirectory 要找的目录
CurrentDir(默认=空调用就可以)
AllFileList (TStringList,查找到的文件列表)
procedure TNetFile.XCollectFiles(ADirectory, CurrentDir: String);
var
Path : String;
Ret : Integer;
DirFile : TSearchRec;
begin
Path := ADirectory+'/*.*';
Ret := FindFirst(Path,faAnyFile,DirFile);

If Ret <> NO_ERROR then Exit;
Try
while Ret = NO_ERROR do
begin
if (DirFile.Name ='.') Or (DirFile.Name = '..') then
begin
Ret := FindNext(DirFile);
Continue;
end;

If (DirFile.Attr and (faDirectory or faVolumeID)) = 0 Then //是文件
AllFileList.Add(CurrentDir+'/'+DirFile.Name) //是目录
else
XCollectFiles(ADirectory+'/'+DirFile.name,CurrentDir+'/'+DirFile.Name);

Ret := FindNext(DirFile);
end;//while do
Finally
FindClose(DirFile);
end;
end;
 
http://www.delphibbs.com/delphibbs/dispq.asp?lid=2121976
 
递归调用。
 

procedure TFormTttNetItemSetFiles.FindAllFiles(sFolderPath: string);
var
SeachPath: string;
FileAttrs: Integer;
Sr: TSearchRec;
S: string;
begin
FileAttrs := faAnyFile;

if sFolderPath[Length(sFolderPath)] = '/' then
SeachPath := sFolderPath + '*.*'
else
SeachPath := sFolderPath + '/*.*';

if FindFirst(SeachPath, FileAttrs, Sr) = 0 then
begin
repeat
if (Sr.Attr = faDirectory) and (Sr.Name <> '.') and (Sr.Name <> '..') then
begin
S := sFolderPath;
if S[Length(S)] <> '/' then
S := S + '/';
S := S + Sr.Name;

Self.FindAllFiles(S);
end;

if Sr.Attr <> faDirectory then
begin
/////////////////

end;
until FindNext(Sr) <> 0;
FindClose(Sr);
end;
end;

 
多人接受答案了。
 
后退
顶部