How to Search a File through all the SubDirectories in Delphi -
i implemented code again not able search through subdirectories .
procedure tffilesearch.filesearch(const dirname:string); begin //we write our search code here if findfirst(dirname,faanyfile or fadirectory,searchresult)=0 begin try repeat showmessage(inttostr(searchresult.attr)); if (searchresult.attr , fadirectory)=0 //the result file //begin lbsearchresult.items.append(searchresult.name) else begin filesearch(includetrailingbackslash(dirname)+searchresult.name); // end; until findnext(searchresult)<>0 findclose(searchresult); end; end; end; procedure tffilesearch.btnsearchclick(sender: tobject); var filepath:string; begin lbsearchresult.clear; if trim(edtmask.text)='' messagedlg('empty input', mtwarning, [mbok], 0) else begin filepath:=cbdirname.text+ edtmask.text; showmessage(filepath); filesearch(filepath); end;
end;
i giving search *.ini files in e:\ drive. filepath e:*.ini. code not search directories in e:\ drive. how correct it?
thanks in advance
you can't apply restriction file extension in call findfirst
. if did directories not enumerated. instead must check matching extension in code. try this:
procedure tmyform.filesearch(const dirname:string); var searchresult: tsearchrec; begin if findfirst(dirname+'\*', faanyfile, searchresult)=0 begin try repeat if (searchresult.attr , fadirectory)=0 begin if sametext(extractfileext(searchresult.name), '.ini') begin lbsearchresult.items.append(includetrailingbackslash(dirname)+searchresult.name); end; end else if (searchresult.name<>'.') , (searchresult.name<>'..') begin filesearch(includetrailingbackslash(dirname)+searchresult.name); end; until findnext(searchresult)<>0 findclose(searchresult); end; end; end; procedure tmyform.formcreate(sender: tobject); begin filesearch('c:\windows'); end;
Comments
Post a Comment