Mega Code Archive

 
Categories / Delphi / Files
 

Klasör ve alt klasörlerde dosya aramak ve bulunanlari bir listeye atmak

{ Form1'in üzerindeki Memo1'e bulunan dosyalari ekler. Not: Kod recursion kullanmakta. Cok fazla (Binlerce) dosya bulundugunda Stack Overflow hatasi verebilir } procedure TForm1.FindFiles(StartDir, FileMask: string); var SR: TSearchRec; DirList: TStringList; IsFound: Boolean; i: integer; begin if StartDir[length(StartDir)] <> '\' then StartDir := StartDir + '\'; IsFound := FindFirst(StartDir+FileMask, faAnyFile-faDirectory, SR) = 0; while IsFound do begin Memo1.Lines.Add(StartDir + SR.Name); IsFound := FindNext(SR) = 0; end; FindClose(SR); DirList := TStringList.Create; IsFound := FindFirst(StartDir+'*.*', faAnyFile, SR) = 0; while IsFound do begin if ((SR.Attr and faDirectory) <> 0) and (SR.Name[1] <> '.') then DirList.Add(StartDir + SR.Name); IsFound := FindNext(SR) = 0; end; FindClose(SR); for i := 0 to DirList.Count-1 do FindFiles(DirList[i], FileMask); DirList.Free; end; // Kullanimi: FindFiles('C:\windows\', '*.txt');