Set*_*gie 4 delphi file find delphi-xe
我试图列出给定目录中的所有目录.我有这个代码:
var
srec: TSearchRec;
begin
// folder is some absolute path of a folder
if FindFirst(folder + PathDelim + '*', faDirectory, srec) = 0 then
try
repeat
if (srec.Name <> '.') and (srec.Name <> '..') then
ShowMessage(srec.Name);
until FindNext(srec) <> 0;
finally
FindClose(srec);
end;
Run Code Online (Sandbox Code Playgroud)
但由于某种原因,我只收到有关文件名而不是目录的消息.我认为使用faDirectory
将使make FindFirst
和family只返回目录的名称.我究竟做错了什么?如果我改成它
if FindFirst(folder, faDirectory, srec) = 0 then
Run Code Online (Sandbox Code Playgroud)
然后它只显示名称folder
但不是绝对路径(相对于folder + '/..'
),然后退出.
我意识到我可以确定它是否是一个目录,(srec.Attr and faDirectory) = faDirectory
但我觉得这是以迂回的方式做事情,应该有一个正确的方法来做到这一点.
如果您使用的是delphi xe,请检查该TDirectory.GetDirectories
功能.
该SysUtils.FindFirst
文档已回答了你的问题.
function FindFirst(const Path: string; Attr: Integer; var F: TSearchRec): Integer;
Run Code Online (Sandbox Code Playgroud)
除了所有普通文件外, Attr参数还指定要包含的特殊文件.指定Attr参数时,请从这些文件属性常量中进行选择.