delphi中如何获得一个文件夹下的所有文件(50分)

  • 主题发起人 主题发起人 manana
  • 开始时间 开始时间
M

manana

Unregistered / Unconfirmed
GUEST, unregistred user!
问题:delphi中如何获得一个文件夹下的所有文件,列出所有文件。点击其中的一个文件名,可以打开这个文件。打开文件的浏览器已经有了。
 
用FindFirst,FindNext遍历一下
 
用FileListBox控件,很方便。
 
delphi 5 开发人员指南 上有详细代码,而且还可以查到子目录。
 
如何判断一个文件夹是否存在,如果没有,如何创建?
 
DirectoryExists函数判断目录是否存在
ForceDirectories函数创建目录,包括父目录
 
procedure TForm1.Button1Click(Sender: TObject);

begin
GetAllFiles('c:/chenhu2');
end;

procedure TForm1.GetAllFiles(SourcePath: string);
var
sr: TSearchRec;
FCount: integer;
begin
FCount := 0;
if DirectoryExists(SourcePath) then
if FindFirst(SourcePath + '/*.*',faAnyFile,sr)=0 then
begin
repeat
if(sr.Name<>'.') and (sr.Name<>'..') then
begin
if sr.Attr<>faDirectory then inc(FCount)
else GetAllFiles(SourcePath + sr.Name)
end;
until FindNext(sr)<>0;
end;
FindClose(sr);
Edit1.Text := inttostr(FCount);
end;
 
function isvalidfile(searchrec: tsearchrec): boolean;
begin
if (searchrec.Attr=16) or (searchrec.Attr=8) then
result:=false
else result:=true;

end;
function searchFile(filepath: string;filelist: tstringlist): boolean;
var searchrec:TSearchrec;
searchstr:string;
begin
if filelist=nil then
begin
result:=false;
exit;
end
else
begin
filelist.Clear;
end;
result:=false;
if copy(filepath,length(filepath),1)<>'/' then
searchstr:=filepath+'/*.*';
if (findfirst(searchstr,faanyfile,searchrec)=0) then
begin
result:=true;
if isvalidfile(searchrec) then
filelist.Add(searchrec.Name);
while (findnext(searchrec)=0) do
begin
if isvalidfile(searchrec) then
filelist.Add(searchrec.Name);
end;
end;
findclose(searchrec);
end;

 
给你两个函数,这是我以前做的,都可以实现:(可以将Memo换成字符串列表)
procedure TForm1.FindAllFiles(var Path: string);
var
Sr: TSearchRec;
Err : Integer;
Oldlen : Integer;
RecPath:string;
begin
RecPath:=Path;
Oldlen := Length(Path);
Err := FindFirst( Path+'*.*',$37,Sr);
while Err=0 Do
begin
If (Sr.Attr and (faDirectory or faVolumeID)) = 0 Then //是文件
begin
Memo1.Lines.Add(Path+Sr.Name);
end;
If ((Sr.Attr and faDirectory)<>0) and (Sr.Name[1] <> '.')Then //是目录
begin
Memo1.Lines.Add(Path+Sr.Name);// ExpandFileName(Sr.Name)
RecPath:= RecPath + Sr.Name + '/';
FindAllFiles(RecPath);
Delete(Recpath,Oldlen+1,256); //
End;
Err := FindNext(Sr);
End;
FindClose(Sr);
end;

procedure TForm1.MakeTree;
var Sr : TSearchRec;
Err : integer;
//FilePath : string;
QuotationIn:Boolean;
begin
Err:=FindFirst('*.*',$37,Sr) ;
While (Err = 0) do
begin
QuotationIn:=True;
if Sr.Name[1]<>'.' then // 如果不是目录
begin
if Pos('''',Sr.Name)<>0 then QuotationIn:=False;
if ((Sr.Attr and faDirectory)=0) and QuotationIn then
begin
Memo2.Lines.Add(ExpandFileName(Sr.Name));
end
end;

if ((Sr.Attr and faDirectory)<>0) and (Sr.Name[1] <> '.') and QuotationIn then //如果是目录
begin
Memo2.Lines.Add(ExpandFileName(Sr.Name));
ChDir(Sr.Name) ;//ChDir(ExpandFileName(Sr.Name))
MakeTree;
ChDir('..') ;
end ;
Err:=FindNext(Sr) ;
Application.ProcessMessages;
end ;
FindClose(Sr);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
Dir:string;
begin
Memo1.Lines.Clear;
if SelectDirectory('请选择光盘的盘符或一个目录:','',Dir) then
if Dir='' then Exit
else
if Dir[Length(Dir)]<>'/' then
begin
Dir:=Dir+'/';
ChDir(Dir);
FindAllFiles(Dir);
end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
Dir:string;
begin
Memo2.Lines.Clear;
if SelectDirectory('请选择光盘的盘符或一个目录:','',Dir) then
if Dir='' then Exit
else
if Dir[Length(Dir)]<>'/' then
begin
Dir:=Dir+'/';
ChDir(Dir);
MakeTree;
end;
end;
 
thanks everyone.
 
后退
顶部