如何取得指定目录内的所有文件的名字,很急!!!(100分)

  • 主题发起人 主题发起人 qdlaoma
  • 开始时间 开始时间
Q

qdlaoma

Unregistered / Unconfirmed
GUEST, unregistred user!
请务必给予解答,否则要下岗了!!!!谢谢!!!!!
 
http://www.gislab.ecnu.edu.cn/delphibbs/DispQ.asp?LID=333130
另,你在帮助理输入:findfirst,findnext
 
告诉你一个简单方法!用OPENDIALOG,过滤中用*。*可以得到奇目录下的所有文件·!
 
该问题问过好多遍了,这是COPY下来的:
unit Unit1;

interface

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

type
TForm1 = class(TForm)
TreeView1: TTreeView;
ListView1: TListView;
Edit1: TEdit;
Button1: TButton;
OpenDialog1: TOpenDialog;
PopupMenu1: TPopupMenu;
N1: TMenuItem;
N2: TMenuItem;
N3: TMenuItem;
procedure Button1Click(Sender: TObject);
procedure TreeView1Click(Sender: TObject);
procedure N1Click(Sender: TObject);
private
function GetTreePath(CurrentNode:TTreeNode):string;
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var i,j,k:integer;
sr: TSearchRec;
Node1:TTreeNode;
s:string;
begin
OpenDialog1.InitialDir :=Edit1.text;
if OpenDialog1.Execute Then
begin
Edit1.Text :=ExtractFilePath(OpenDialog1.Filename);
TreeView1.Items.Clear;
Node1:=TreeView1.Items.Add(nil,Edit1.text);
i:=0;
while i'.') and (sr.name<>'..') then
TreeView1.items.AddChild(node1,sr.name+'/');
k:=FindNext(sr);
end;
if j=0 then FindClose(sr);
i:=i+1;
end;
TreeView1.FullExpand;
end;
end;
function TForm1.GetTreePath(CurrentNode:TTreeNode):string;
var
ParentNode:TTreeNode;
begin
result:=CurrentNode.text;
ParentNode:=CurrentNode.Parent;
while ParentNode<>nil do
begin
result:=ParentNode.text+result;
ParentNode:=ParentNode.Parent;
end;
end;
procedure TForm1.TreeView1Click(Sender: TObject);
var i,j:integer;
sr: TSearchRec;
Node:TTreeNode;
s:string;
NewColumn: TListItem;
begin
if TreeView1.selected=nil then exit;
s:=GetTreePath(TreeView1.selected)+'*.*';
ListView1.Items.Clear;
j:=FindFirst(s,0,sr);
i:=j;
while i=0 do
begin
NewColumn:=ListView1.items.Add;
NewColumn.Caption:=sr.name;
i:=FindNext(sr);
end;
if j=0 then FindClose(sr);
end;

procedure TForm1.N1Click(Sender: TObject);
var
Node:TTreeNode;
s:string;
begin
With TreeView1 do begin
Node:=Selected;
s:=IntToStr(random(MaxInt));
Node:=items.AddChild(node,s);
Selected:=Node;
end;
end;

end.
 
给你发了个很好用的TFindFile。
 
还要取得子目录的名字!!拜托了!!!
 
大家这么喜欢copy,我也copy一段,是生成ie收藏夹菜单时用递归的笨方法编历收藏夹目录下的目录和文件。
知道如何遍历就成了
procedure tfrmhtmlbrowser.addfavoritesmenu(menuitem: TMenuItem; favoritespath: string);
var
favoritefilename: string;
Search: TSearchRec;
submenuitem: TMenuItem;
i, j: integer;
begin
if findfirst(favoritespath + '/*.*', faArchive or faDirectory, search) = 0 then
repeat
if (search.Name <> '.') and (search.Name <> '..') then
begin
submenuitem := TMenuItem.Create(Self);
if (search.Attr and fadirectory) = fadirectory then
begin//如果是目录的话,就递归调用编历目录
submenuitem.Caption := search.Name;
submenuitem.ImageIndex := 0;
menuitem.Add(submenuitem);
addfavoritesmenu(submenuitem, favoritespath + '/' + search.Name);
end
else
begin
i := Pos('.LNK', uppercase(search.Name));
j := Pos('.URL', UpperCase(search.Name));
favoritefilename := favoritespath + '/' + search.Name;
submenuitem.Hint := GetFavoritesUrl(favoritefilename);
if (i <> 0) then
submenuitem.Caption := Copy(search.Name, 0, i - 1);
if (j <> 0) then
submenuitem.Caption := Copy(search.Name, 0, j - 1);
submenuitem.ImageIndex := 1;
submenuitem.OnClick := menuclick;
menuitem.Add(submenuitem);
addfavoritesmenu(submenuitem, favoritespath + '/' + search.Name);
end;
end;
until findnext(search) <> 0;
findclose(search);
end;
 
多人接受答案了。
 
后退
顶部