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;