如何获得目录及目录下子目录下的所有文件,并循环出来?(0分)

  • 主题发起人 主题发起人 xbbtzhao
  • 开始时间 开始时间
X

xbbtzhao

Unregistered / Unconfirmed
GUEST, unregistred user!
如何获得目录及目录下子目录下的所有文件,并循环出来?
 
下面是一个进行目录搜索的例程,经过修改你可以搜索需要的文件类型

procedure searchpath(path:string);
var
SearchRec : TSearchRec;
ext : string; //后缀
begin
//在路径后面加上'/'
if path[Length(path)] <> '/' then path := path + '/';
if FindFirst(path + '*.*', faAnyFile, SearchRec) <> 0 then Exit;
//判断是否为目录,是目录则循环
if (SearchRec.Attr and faDirectory) <> 0 then
begin
if (SearchRec.name<>'.') and (SearchRec.Name<>'..') then
SearchPath(path+searchrec.name);
end
else
begin
{ 此处加上你自已的判断文件后缀的代码
ext := LowerCase(ExtractFileExt( SearchRec.Name ));
if (ext='.bmp') or (ext='.jpg') then
begin
...
end;}
end;
while FindNext(SearchRec)=0 do
begin
if (SearchRec.Attr and faDirectory) <> 0 then
begin
if (SearchRec.Name<>'.') and (SearchRec.Name<>'..') then
SearchPath(path+SearchRec.Name);
end
else
begin
{ 此处加上你自已的判断文件后缀的代码
ext := LowerCase(ExtractFileExt( SearchRec.Name ));
if (ext='.bmp') or (ext='.jpg') then
begin
...
end;}
end;
end;
FindClose(SearchRec);
end;
 
朋友,谢了,
 
用递归代码少多了~~~
 
简单迭代就OK了
 
楼上的朋友,简单就给段代码,谢了
 
我的一段代码.删除路径及路径下所有文件,和你的要求差不多,希望对你有帮助

{ 删除文件夹及子文件夹中的文件 }
{ 2005-10-14 14:23 by muhx}
procedure DeleteDir(const ADirName: string);
var
tmpSearchRec: TSearchRec;
tmpDir: string;
begin
tmpDir := IncludeTrailingPathDelimiter(ADirName);
if not DirectoryExists(tmpDir) then
Exit;
if FindFirst(tmpDir + '*.*', faAnyFile , tmpSearchRec) = 0 then
begin
repeat
if ((tmpSearchRec.Attr and SysUtils.faReadOnly) <> 0) or
((tmpSearchRec.Attr and SysUtils.faHidden) <> 0) then
SetFileAttributes(PChar(tmpDir + tmpSearchRec.Name), FILE_ATTRIBUTE_NORMAL);
if (tmpSearchRec.Attr = faDirectory + SysUtils.faReadOnly) or
(tmpSearchRec.Attr = faDirectory + SysUtils.faHidden) or
((tmpSearchRec.Attr = faDirectory) and (tmpSearchRec.Name <> '.') and (tmpSearchRec.Name <> '..')) then
begin
System.ChDir(tmpSearchRec.Name);
DeleteDir(tmpDir + tmpSearchRec.Name);
System.ChDir('..');
end;
Windows.DeleteFile(PChar(tmpDir + tmpSearchRec.Name));
until FindNext(tmpSearchRec) <> 0;

Windows.FindClose(tmpSearchRec.FindHandle);
RemoveDir(tmpDir);
end;
end;
 
偶也贴个..在窗体上换一个ListBox1和Button1控件.就可以目录下所有文件路径显示在ListBox1列表里面.


unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;

type
TForm1 = class(TForm)
ListBox1: TListBox;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

TSearchFileThread=class(TThread)
private
MyFileName: string;
procedure SearchFile(DirName: string);
procedure DeelWithFile;
procedure SearchComplete;
public
MyDirName: string;
protected
procedure Execute; override;

end;

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TSearchFileThread.DeelWithFile;
begin
Form1.ListBox1.Items.Insert(0, MyFileName);
end;

procedure TSearchFileThread.SearchFile(DirName: String);
Var
Found: integer;
SearchRec: TSearchRec;
begin
Found := FindFirst(DirName + '*.*',faAnyFile,searchrec);
while Found = 0 do
begin
if ((SearchRec.Attr and faDirectory)<>0) then //directory
begin
if(SearchRec.Name <> '.')and(SearchRec.Name <> '..') then
SearchFile(DirName + SearchRec.Name + '/')
end
else //file
begin
MyFileName := DirName + SearchRec.Name;
Synchronize(DeelWithFile);
end;
Found := FindNext(SearchRec);
end;
FindClose(SearchRec);
end;

procedure TSearchFileThread.SearchComplete;
begin
ShowMessage('搜索完毕,共找到' + inttostr(Form1.ListBox1.Items.Count) + '个文件');
end;

procedure TSearchFileThread.Execute;
begin
SearchFile(MyDirName);
Synchronize(SearchComplete);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
SearchFileThread: TSearchFileThread;
begin
SearchFileThread := TSearchFileThread.Create(true);
SearchFileThread.MyDirName := 'F:/'; //必须以 '/' 结尾
SearchFileThread.Resume;
end;

end.
 
谢谢楼上的兄弟
 
对了,下次问问题记得给分
 
谢谢楼上的兄弟,我给你加份还想问个问题?
Found := FindFirst(DirName + '*.*',faAnyFile,searchrec);//如果只打*.doc等,就找不到子目录的文件了,
 
答案写在你的新贴子里.自已去看.
 
后退
顶部