列举当前目录下的所有一级子目录(100分)

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

smilboy

Unregistered / Unconfirmed
GUEST, unregistred user!
.
..
[1]
[2]
[3]
file1
file2

我要的是[1],[2],[3]

下面的代码不成功

function IsValidDir(SearchRec:TSearchRec):Boolean;
begin
if (SearchRec.Attr=16) and
(SearchRec.Name<>'.') and
(SearchRec.Name<>'..') then
Result:=True
else
Result:=False;
end;

procedure TForm1.Button1Click(Sender: TObject);
var
SearchRec: TSearchRec;
begin
if (FindFirst(edit1.Text, faDirectory, SearchRec)=0) then
begin
if IsValidDir(SearchRec) then
memo1.lines.add(SearchRec.Name);
while (FindNext(SearchRec) = 0) do
begin
if IsValidDir(SearchRec) then
memo1.Lines.Add(SearchRec.Name);
end;
end;
FindClose(SearchRec);
end;
 
procedure TForm1.Button1Click(Sender: TObject);
var
sr :TSearchRec;
fileDate: TDateTime;
begin
if FindFirst('f:/downloads/*.*',faDirectory,sr) = 0 then
begin
if (sr.Attr and faDirectory)=sr.Attr then
begin
FileDate := FileAge(sr.name);
Listbox1.Items.Add(sr.name);//+' '+FileDatetoDateTime(FileDate));
end;
while FindNext(sr) = 0 do
begin
if (sr.Attr and faDirectory)=sr.Attr then
Listbox1.Items.Add(sr.name+' '+datetimeTostr((FileAge(sr.name))));
end;
end;
end;
//你自己去掉.和..好了
 
如果你需要所有子目录的话,我也可以给你
 
1.FindFirst的第一个参数有问题。比如要找C盘下的所有子目录,应该写成c:/*.*而不能单写一个c:或是c:/
2.if (SearchRec.Attr=16)... 有问题。应该写成if (SearchRec.Attr and 16 = 16)...即按位操作。

你再试试。
 
procddure IsDirNotation(DirName: String): Boolean;
begin
Result := (DirName = '.') or (DirName = '..');
end;

ResRec: TSearchRec;

FindResult := FindFirst(Path + '*.*', faDirectory, ResRec);
ResRec包含目录信息

while FindResult = 0 do
begin
if ((ResRec.Attr and faDirectory) = faDirectory) and not
IsDirNotation(ResRec.Name) //判断不为'.','..'子目录
then
//do your job.
FindResult := FindNext(ResRec);
end;
 
看 Vcl 源代码也行
 
接受答案了.
 
后退
顶部