查找目录下所有子目录下的所有文件!!(100分)

  • 主题发起人 主题发起人 sy_luoping
  • 开始时间 开始时间
S

sy_luoping

Unregistered / Unconfirmed
GUEST, unregistred user!
小弟有一事相求,就是我想读取目录下的所有图片文件,包括子目录的,在网上有一些方法,但是没有调用方式,我用网上的方法实验了下,没有成功,谁有实例和调用方式,能否提供给我,万分感谢!!!
 
unit Unit1;

interface

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

type
TForm1 = class(TForm)
Edit1: TEdit;
Label1: TLabel;
Button1: TButton;
ListBox1: TListBox;
Edit2: TEdit;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
function IsValidDir(SearchRec:TSearchRec):Boolean;
function SearchFile(mainpath:string;filename:string;var foundresult:TStrings):Boolean;
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

{ TForm1 }

function TForm1.IsValidDir(SearchRec: TSearchRec): Boolean;
begin
if (SearchRec.Attr=16) and (SearchRec.Name<>'.') and (SearchRec.Name<>'..') then
result:=true
else
result:=false;
end;

function TForm1.SearchFile(mainpath, filename: string;
var foundresult: TStrings): Boolean;
var
i:Integer;
Found:Boolean;
subdir1:TStrings;
searchRec:TSearchRec;
begin
Found:=false;
if trim(filename)<>'' then
begin
subdir1:=TStringList.Create;
if (FindFirst(mainpath+'*.*',faDirectory,searchRec)=0) then
begin
if IsValidDir(SearchRec) then
subdir1.Add(SearchRec.Name);
while (FindNext(SearchRec)=0 ) do
begin
if IsValidDir(SearchRec) then
subdir1.Add(SearchRec.Name);
end;
end;
FindClose(SearchRec);
if FileExists(mainpath+filename) then
begin
Found:=true;
foundresult.Add(mainpath+filename);
listbox1.Items.Add(mainpath+filename);
end;
for i:=0 to subdir1.Count-1 do
found:=SearchFile(mainpath+subdir1.Strings+'/',FileName,foundresult);
subdir1.Free;
end;
result:=found;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
s:TStrings;
i:integer;
begin
s:=TStringList.Create;
SearchFile(edit1.Text,edit2.Text,s);
ShowMessage(inttostr(s.Count));
ListBox1.Items.AddStrings(s);
s.Free;
end;

end.

需要完整文件名
如D:/ test.txt 测试通过
 
能告诉我,第二个参数你传的是什么吗?我传的是'*.jpg',查询出来的是0条啊!!
 
我要查询的是文件夹里所有的图片文件,不能是单个文件。
 
文件名啊
 
所有文件名呢,只能提供后缀名啊。
 
楼上,我按照你的方式做了,如果查找单个文件,能查到,但是所有文件不行啊。能否告诉如果查找所有文件,怎样做吗?
 
没干过,把这的判断条件改了,吃饭去了,回来再研究
if FileExists(mainpath+filename) then
begin
Found:=true;
foundresult.Add(mainpath+filename);
listbox1.Items.Add(mainpath+filename);
end;
 
DIGUA,你有qq吗?
在这里说话太费劲了。
 
我测试过了,没问题了,自己再优化下吧

借助了FileListBox控件

可以的话,结帖给分哦!!! 呵呵

unit Unit1;

interface

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

type
TForm1 = class(TForm)
Edit1: TEdit;
Label1: TLabel;
Button1: TButton;
ListBox1: TListBox;
Edit2: TEdit;
OpenDialog1: TOpenDialog;
FileListBox1: TFileListBox;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
function IsValidDir(SearchRec:TSearchRec):Boolean;
function SearchFile(mainpath:string;filename:string;var foundresult:TStrings):Boolean;
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

{ TForm1 }

function TForm1.IsValidDir(SearchRec: TSearchRec): Boolean;
begin
if (SearchRec.Attr=16) and (SearchRec.Name<>'.') and (SearchRec.Name<>'..') then
result:=true
else
result:=false;
end;

function TForm1.SearchFile(mainpath, filename: string;
var foundresult: TStrings): Boolean;
var
i:Integer;
Found:Boolean;
subdir1:TStrings;
searchRec:TSearchRec;
s:string;
begin
Found:=false;
if trim(filename)<>'' then
begin
subdir1:=TStringList.Create;
if (FindFirst(mainpath+'*.*',faDirectory,searchRec)=0) then
begin
if IsValidDir(SearchRec) then
subdir1.Add(SearchRec.Name);
while (FindNext(SearchRec)=0 ) do
begin
if IsValidDir(SearchRec) then
subdir1.Add(SearchRec.Name);
end;
end;
FindClose(SearchRec);

FileListBox1.Visible:=false;
FileListBox1.Mask:='*'+filename;
FileListBox1.Directory:=mainpath;

for i:=0 to FileListBox1.Items.Count-1 do
begin
if FileExists(FileListBox1.Items.Strings) then
s:= FileListBox1.Items.Strings;
begin
Found:=true;
foundresult.Add(mainpath+s);
end;
end;

for i:=0 to subdir1.Count-1 do
found:=SearchFile(mainpath+subdir1.Strings+'/',FileName,foundresult);
subdir1.Free;
end;
result:=found;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
s:TStrings;
begin
s:=TStringList.Create;
SearchFile(edit1.Text,edit2.Text,s);
ListBox1.Items.AddStrings(s);
s.Free;
end;


end.
 
我想再问下,当我传递参数的时候,第二个参数我可以写成'*.jpg'吗?就是说我需要查到所有。jpg文件
 
是的
这段改下,刚才把查完整文件名部分去了
你可以这样查 web.xml 或 .xml 都没问题

if pos('.',filename)>0 then
begin
FileListBox1.Visible:=false;
FileListBox1.Mask:='*'+filename;
FileListBox1.Directory:=mainpath;

for i:=0 to FileListBox1.Items.Count-1 do
begin
if FileExists(FileListBox1.Items.Strings) then
s:= FileListBox1.Items.Strings;
begin
Found:=true;
foundresult.Add(mainpath+s);
end;
end;
end
else
begin
if FileExists(mainpath+filename) then
begin
Found:=true;
foundresult.Add(mainpath+s);
end;
end;
 
因为测试原因 有些代码多余的 可以优化下

不可以就说话,可以就接分咯
 
谢谢了!!!
 
后退
顶部