关于FindFirst、FindNext
以下是找出所有当前目录的子目录的一段代码,可是不能成功,请各位大侠指教!
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;
请帮忙,我在线恭候!!
问题点数:20、回复次数:6Top
1 楼netlib(河外孤星)回复于 2002-07-01 10:54:46 得分 0
这是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;Top
2 楼xiaofeng_cxy(萧风)回复于 2002-07-01 11:16:25 得分 0
你这说明的只是查找文件啊,我想找出当前目录下所有的子目录啊!!
Top
3 楼PoolD(池龙)回复于 2002-07-01 11:27:37 得分 20
用来查找子目录的函数,结果返回到一个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;
Top
4 楼xiaofeng_cxy(萧风)回复于 2002-07-01 11:58:22 得分 0
我只想知道我的代码中到底有什么问题呢?
能不能不要答非所问啊?
Top
5 楼xiaofeng_cxy(萧风)回复于 2002-07-01 15:03:25 得分 0
我知道了,是因为没有在目录字符串后加'\'
谢谢各位Top
相关问题
- FindClose是用来终结FindFirst还是FindNext?
- findfirst,findnext,findclose是不是ANSI C函数
- findfirst(), findnext() 在哪个.h 文件定义?
- 用findfirst 和 findnext怎么样区分文件和目录?
- 在win2000用FindFirst,FindNext等出现的问题?
- FindFirst()和FindNext的用法!|!高手过来帮一下!
- 有FindFirst、FindNext遍历文件函数的Unicode版本吗?
- 如何区别用Findfirst和Findnext查找到的是文件还是文件夹????
- FindFirst(...,faAnyFile,sr)和FindNext(sr)为什么只返回了目录,不返回文件?
- 在dos.h找不到_find_t结构,_dos_findfirst,_dos_findnext,_dos_getfileattr等同类函数.




