下面的代码搜索出文件的路径和名称,分别列举到两个列表框中,你也可以直接写入
数据库中,在这里查找的是C:/windows目录下的所有文件的路径和名称,路径在
ListBox1中,文件名称在Listbox2中,窗体上组件有:Button1,Listbox1,ListBox2
procedure TForm1.searchpath(path: string);
var
searchrec: TSearchRec;
begin
//ÔÚ·¾¶ºóÃæ¼ÓÉÏ'/'
if path[length(path)] <> '/' then
path := path + '/';
if FindFirst(path + '*.*', faAnyFile, SearchRec) <> 0 then exit;
//&Aring;&ETH;&para;&Iuml;&Ecirc;&Ccedil;·&ntilde;&Icirc;&ordf;&Auml;&iquest;&Acirc;&frac14;&pound;&not;&Ecirc;&Ccedil;&Auml;&iquest;&Acirc;&frac14;&Ocirc;ò&Ntilde;&shy;&raquo;·
if (searchrec.Attr and faDirectory) <> 0 then
begin
if (searchrec.name <> '.') and (searchrec.name <> '..') then
searchpath(path + searchrec.name);
end
else
begin
ListBox1.Items.Add(path + searchrec.name);
lIstBox2.Items.Add(searchrec.name);
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
ListBox1.Items.Add(path + searchrec.name);
lIstBox2.Items.Add(searchrec.name);
end;
end;
findclose(searchrec);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
searchpath('C:/windows');
end;