列出指定目录下的所有文件
(测试OK!)
procedure ListDir(Path:String; List:TListBox);
{
Path : 起始路径字符串
List : 存放文件名的 List box
}
var
SearchRec:TsearchRec;
Result:integer;
S:string; { 用以保存当前目录, GetDir(0,s) }
begin
try {Exception handler }
ChDir(Path);
except on EInOutError do
begin
MessageDlg('改变目录时发生错误',mtWarning,[mbOK],0);
Exit;
end;
end;
if length(path)<> 3 then path:=path+'/';
FindFirst(path+'*.*',faAnyFile,SearchRec);
Repeat
if SearchRec.Attr=faDirectory then
begin
if (SearchRec.Name<>'.') and (SearchRec.Name<>'..') then
begin
GetDir(0,s);
if length(s)<>3 then s:=s+'/';
List.Items.Add(s+SearchRec.Name);
ListDir(s+SearchRec.Name,List);
end;
end
else
begin
GetDir(0,s);
if length(s)<>3 then List.items.add(s+'/'+SearchRec.Name)
else List.items.add(s+SearchRec.Name);
end;
Result:=FindNext(SearchRec);
Application.ProcessMessages;
until result<>0;
GetDir(0,s);
if length(s)<>3 then ChDir('..');
end;