已知一目录,怎样取出该目录下的所有文件,把文件名放入一数组?(50分)

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

mxsheng

Unregistered / Unconfirmed
GUEST, unregistred user!
已知一目录,怎样取出该目录下的所有文件,把文件名放入一数组?
有现成的函数吗?
有WinAPI函数吗?
 
使用findfirst和findnext
var
sr:tsearchrec;
files:TStringList;
Found:Boolean;
begin
files:=TStringList.Create;
Found:=FindFirst('',faAnyFile,sr);
try
while found=0 do
begin
Files.Items.Add(sr.FileName);
Found:=FindNext(sr);
end;
finally
sr.Free;
end;
end;
 
我使用WinApi可以使用SHGetFileInfo
 
可以用API 函数 FindFirstFile()和FindNextFile()
定义如下:
HANDLE FindFirstFile(

LPCTSTR lpFileName, // pointer to name of file to search for
LPWIN32_FIND_DATA lpFindFileData // pointer to returned information
);

BOOL FindNextFile(

HANDLE hFindFile, // handle to search
LPWIN32_FIND_DATA lpFindFileData // pointer to structure for data on found file
);

其中FindFirstFile中的lpFileName是你要找的文件名,支持通配符,
如果要找一个目录下的所有文件,可以用通配符 * 。
 
没有,自己写。好简单的。你看看我写的这个例子.是类似Explorer的程序。可能对你有收获。
如果觉得满意,给分我吧。我没钱提一个比较难的问题了。
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<TreeView1.items.count do
begin
Node1:=TreeView1.Items.Item;
s:=GetTreePath(Node1);
j:=FindFirst(s+'*.*',faDirectory,sr);
k:=j;
while k=0 do
begin
if (sr.attr and faDirectory) = faDirectory then
if (sr.name<>'.') 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.
 
procedure TForm1.Button1Click(Sender: TObject);
var
hFile:THandle;
WFD:TWIN32FINDDATA;
Ret:Boolean;
begin
Ret:=True;
hFile:= FindFirstFile('C:/Windows/*.exe', WFD);
If hFile<> INVALID_HANDLE_VALUE Then
begin
memo1.Lines.Add (WFD.cFileName);
while Ret do
begin
Ret:=FindNextFile(hFile, WFD);
memo1.Lines.Add (WFD.cFileName);
end;
end;
Windows.FindClose(hFile);
end;
 

都这么多拉~~~~~
 
多人接受答案了。
 
后退
顶部