关于FindFirst、FindNext

xiaofeng_cxy 2002-07-01 10:51:49
以下是找出所有当前目录的子目录的一段代码,可是不能成功,请各位大侠指教!
procedure TForm1.Button2Click(Sender: TObject);
var
SearchRec:TSearchRec;
begin
if FindFirst(DirectoryListBox1.Directory+'*.*',faDirectory,SearchRec)=0 then
begin
lbFiles.Items.Add(SearchRec.Name);

//以下的while循环不能执行?????
while FindNext(SearchRec)=0 do
begin
lbFiles.Items.Add(SearchRec.Name);
ShowMessage(SearchRec.Name);
end;
end;
FindClose(SearchRec);
end;

请帮忙,我在线恭候!!
...全文
130 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiaofeng_cxy 2002-07-01
  • 打赏
  • 举报
回复
我只想知道我的代码中到底有什么问题呢?
能不能不要答非所问啊?
PoolD 2002-07-01
  • 打赏
  • 举报
回复
用来查找子目录的函数,结果返回到一个TStringList中。

function DirsInDir(Dir: string; Mask: string; var DirNames: TStringList; FullDirName: boolean = false): boolean;
//Find Directory in Dir
var
FindHandle: THandle;
FindData: Win32_Find_Data;
procedure Add;
var
vDirName, vFullDirName: string;
begin
{ if FileExists(Dir + FindData.cFileName)
and (FindData.dwFileAttributes and FILE_ATTRIBUTE_DIRECTORY = 0)
and (FindData.dwFileAttributes and FILE_ATTRIBUTE_OFFLINE = 0)
and (FindData.dwFileAttributes and FILE_ATTRIBUTE_TEMPORARY = 0)
then}
vDirName := FindData.cFileName;
vFullDirName := Dir + vDirName;
if not((vDirName = '.') or (vDirName = '..') or (Pos(':\RECYCLED', Uppercase(vFullDirName)) > 0))
and DirectoryExists(vFullDirName) then begin
if FullDirName then
DirNames.Add(vFullDirName)
else
DirNames.Add(vDirName);
end;
end;
begin
Result := false;
Dir := IncludeTrailingBackSlash(Dir);
DirNames.Clear;
FindHandle := FindFirstFile(PChar(Dir + Mask), FindData);
if FindHandle <> 0 then begin
Add;
While FindNextFile(FindHandle, FindData) do Add;
Windows.FindClose(FindHandle);
if DirNames.Count > 0 then Result := true;
end;
end;
xiaofeng_cxy 2002-07-01
  • 打赏
  • 举报
回复
你这说明的只是查找文件啊,我想找出当前目录下所有的子目录啊!!
netlib 2002-07-01
  • 打赏
  • 举报
回复
这是delphi的例子,你看看
procedure TForm1.Button1Click(Sender: TObject);

var
sr: TSearchRec;
FileAttrs: Integer;
begin
StringGrid1.RowCount := 1;
if CheckBox1.Checked then
FileAttrs := faReadOnly
else
FileAttrs := 0;
if CheckBox2.Checked then
FileAttrs := FileAttrs + faHidden;
if CheckBox3.Checked then
FileAttrs := FileAttrs + faSysFile;
if CheckBox4.Checked then
FileAttrs := FileAttrs + faVolumeID;
if CheckBox5.Checked then

FileAttrs := FileAttrs + faDirectory;
if CheckBox6.Checked then
FileAttrs := FileAttrs + faArchive;
if CheckBox7.Checked then

FileAttrs := FileAttrs + faAnyFile;

with StringGrid1 do
begin
RowCount := 0;

if FindFirst(Edit1.Text, FileAttrs, sr) = 0 then

begin
repeat
if (sr.Attr and FileAttrs) = sr.Attr then
begin
RowCount := RowCount + 1;
Cells[1,RowCount-1] := sr.Name;
Cells[2,RowCount-1] := IntToStr(sr.Size);
end;
until FindNext(sr) <> 0;
FindClose(sr);
end;
end;
end;
xiaofeng_cxy 2002-07-01
  • 打赏
  • 举报
回复
我知道了,是因为没有在目录字符串后加'\'
谢谢各位

5,392

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧