西西软件园多重安全检测下载网站、值得信赖的软件下载站!
软件
软件
文章
搜索

首页编程开发Delphi → delphi 中文件查找API函数 findfirst,findnext,findclose 的使用

delphi 中文件查找API函数 findfirst,findnext,findclose 的使用

前往专题相关软件相关文章发表评论 来源:本站整理时间:2010/10/28 17:39:30字体大小:A-A+

作者:不详点击:1704次评论:0次标签: delphi

Borland Delphi8.0光盘版
  • 类型:编程工具大小:83.1M语言:中文 评分:4.0
  • 标签:
立即下载

我们先来看一下需求.

我们需要查找某一个目录下面,体积最大的 exe 文件。

可以用下面的函数:

 K:=FindFirst(AppRootPath+'\*.exe',faAnyFile, vSearchRec);
    while K = 0 do
     begin
       if (AppRootFileName <>  vSearchRec.Name) and (i< vSearchRec.Size) then
       begin
        i:=vSearchRec.Size;
        AppName:= vSearchRec.Name;
       end;
       K:= FindNext(vSearchRec);
     end;

这里用到了两个函数:

FindFirst 是用来寻找目标目录下的第一个文件,
FindFirst函数在delphi帮助下的定义:
function FindFirst(const Path: string; Attr: Integer; var F: TSearchRec): Integer;
其中有一句:FindFirst returns 0 if a file was successfully located
也就是说,当成功找到文件时,返回0.
 
Delphi syntax:

function FindNext(var F: TSearchRec): Integer;

FindNext 则是寻找下一个
TSearchRec 是一个文件信息的纪录,当FindFirst返回SearchRec时,你可以通过SearchRec.Name获取文件名,以及 SearchRec.Size获取文件大小等信息。

The following example uses an edit control, a button, a string grid, and seven check boxes. The check boxes correspond to the seven possible file attributes. When the button is clicked, the path specified in the edit control is searched for files matching the checked file attributes. The names and sizes of the matching files are inserted into the string grid.

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;

 

    相关评论

    阅读本文后您有什么感想? 已有人给出评价!

    • 8 喜欢喜欢
    • 3 顶
    • 1 难过难过
    • 5 囧
    • 3 围观围观
    • 2 无聊无聊

    热门评论

    最新评论

    发表评论 查看所有评论(0)

    昵称:
    表情: 高兴 可 汗 我不要 害羞 好 下下下 送花 屎 亲亲
    字数: 0/500 (您的评论需要经过审核才能显示)