Delphi——FindFirst学习

来源:互联网 发布:怎么查看淘宝卖家电话 编辑:程序博客网 时间:2024/05/29 18:55

FindFirst在SysUtils中声明 原型为
Fucntion FindFirst(const Path:String;Attr:Integer;Var F:TSearchRec):Integer;

描述:
从指定的目录中搜索第一个给定属性文件实例
返回结果保存到参数F中,F参数是一个文件结构包含文件信息
成功返回0,否则返回错误代码

参数Path:包含路径和搜索模糊文件名,包括通配符: './text/*.*'搜索test目录下的全部文件
参数Attr:指定文件属性类型
     faReadOnly:只读文件
     faHidden:隐藏文件
     faSysFile:系统文件
     faVolumeID:卷标文件
     faDirectory:目录文件
     faArchive:归档文件//压缩文件
     faAnyFile:任何文件

Attr可以通过组合使用 faReadOnly+faSysFile

注意:
使用FindFist是要分配内存的使用完之后必须用FindClose关闭
一些属性只能在特定的平台上使用 faVolumeID和faArchivei不能在Unix和mac上使用

//遍历文件夹下所有文件function TForm1.GetFileList(ASourFile: string): TStringList;var sour_path,sour_file: string;    TmpList:TStringList;    FileRec:TSearchrec;begin   sour_path:=ExtractFilePath(ASourFile);   sour_file:=ExtractFileName(ASourFile);   TmpList:=TStringList.Create;   TmpList.Clear;   if DirectoryExists(sour_path) then   begin     if FindFirst(sour_path+sour_file,faAnyfile,FileRec) = 0 then     repeat        if ((FileRec.Attr and faDirectory) = 0) then           begin             TmpList.Add(sour_path+FileRec.Name);           end;     until FindNext(FileRec)<>0;     SysUtils.FindClose(FileRec);   end;   result := TmpList;end;

原创粉丝点击