How to write a program that can search files

Thursday, November 3, 2011 0 comments
In order to search a file we will use FindFirst functions i.e. defined in sysutils class(unit).FindFirst will search the first file then the function FindNext is used to search other file if it contains with same name. 
 
function FindFirst(const Path: string; Attr: Integer; var Rec: TSearchRec): Integer;


Here three parameter is passed in the FindFirst function and it returns the integer. The first parameter passed is the path of file along with the name of the file to be searched. We can extract the current path using the function extractfilepath function and on that reference we can search file as normally necessary file is located in same path of application file. 
 
function ExtractFilePath(const S: FullFileName): string;

On passing the file name to the function extractfilepath it will return the drive and directory parts of a string containing full path and file name.
The second parameter passed is integer. We need to pass the integer value or constant name as defined below for the required task we need to perform i.e. if we need to search readonly files we can sent either 1 or constant faReadOnly. On similar ways we can search other things
Constant
   Value
    Description
faReadOnly
  1
    Read-only files
faHidden
  2
    Hidden files
faSysFile
  4
    System files
faVolumeID
  8
    Volume ID files
faDirectory
  16
    Directory files
faArchive
  32
    Archive files
faSymLink
  64
    Symbolic link
faAnyFile
  71
    Any file

How file is searched then 
_application_path := extractfilepath(application.exename);
if FindFirst(_application_path + '*.otf', 0, sr) = 0 then
begin
 repeat
 ……………
 until 
FindNext(sr)<>0;
FindClose(sr);
end;

In above program the path is extracted using the function extractfilepath, application.exename gives the name of the current application name. 
Here the filename with extension .otf is searched as it is passed as a parameter in function. If the function FindFirst found any file it will return the value 0 else it will return error with other integer value(Normally 18).Here sr is a variable of type TSearchRec.
TSearchRec is a record type i.e. collection or bunch of variable. 


The TSearchRec is a record defined as:
Type
TsearchRec=record
Time :       Integer;
Size :       Integer;
Attr :       Integer;
Name :       TFileName;
ExcludeAttr :Integer;
FindHandle : THandle;
FindData :   Twin32FindData;
End;

If the file is found running above code the file information is stored in the record Tsearchrec i.e sr variable. Then we can access the information of file i.e name,size on using the TsearchRec record for eg if we found the file a.otf inorder to get name of file we will use sr.Name. Similary we can access other information.

While searching a file it need to run in a loop. The program start with the function FindFirst then it enters in the loop and the FindNext function will run until it finds the file searched.


Inorder to terminate the call for a FindFirst/FindNext we call the function FindClose
procedure FindClose(var Rec: TSearchRec) ; 



Links for detail study(References)

·         http://docwiki.embarcadero.com/VCL/en/SysUtils.TSearchRec
·         http://delphi.about.com/od/vclusing/a/findfile.htm
·         http://delphi.about.com/library/rtl/blrtlExtractFilePath.htm



0 comments:

Post a Comment

Recent Posts

WebInfo

 

©Copyright 2011 WebInfo