• 微软原版系统

  • 一键重装系统

  • 纯净系统

  • 在线技术客服

魔法猪系统重装大师 一键在线制作启动 U 盘 PE 系统 用一键重装的魔法拯救失去灵魂的系统
当前位置:首页 > 教程 > 电脑教程

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

时间:2015年04月02日 15:42:09    来源:魔法猪系统重装大师官网    人气:4415
前往Delphi专题

我们先来看一下需求.

我们需要查找某一个目录下面,体积最大的 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;

 

delphi,中,文件,查找,API,函数,findfirs
栏目:电脑教程 阅读:1000 2023/12/27
Win7教程 更多>>
U盘教程 更多>>
Win10教程 更多>>
魔法猪学院 更多>>

Copyright © 2015-2023 魔法猪 魔法猪系统重装大师

本站发布的系统仅为个人学习测试使用,请在下载后24小时内删除,不得用于任何商业用途,否则后果自负,请支持购买微软正版软件。

在线客服 查看微信 返回顶部