Use of the file search API functions findfirst, findnext, and findclose in Delphi

Source: Internet
Author: User

 

We need to find the largest EXE file under a directory.

You can use the following functions:

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;

Two functions are used here:

Findfirst is used to find the first file in the target directory,

The findfirst function is defined with the help of Delphi:
Function findfirst (const path: string; ATTR: integer; var F: tsearchrec): integer;

One sentence is: findfirst returns 0 if a file was successfully located
That is to say, when the file is successfully found, 0 is returned.

 

Delphi Syntax:

Function findnext (var f: tsearchrec): integer;

Findnext is to find the next
Tsearchrec is a file information record. When findfirst returns searchrec, you can use searchrec. Name to obtain the file name and searchrec. Size to obtain the file 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
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;

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.