Guest User

Untitled

a guest
Jun 19th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.55 KB | None | 0 0
  1. unit findfile;
  2.  
  3. interface
  4.  
  5. uses
  6. Classes;
  7.  
  8. type
  9. TFileAttrKind = (ffaReadOnly, ffaHidden, ffaSysFile, ffaDirectory, ffaArchive, ffaAnyFile);
  10. TFileAttr = set of TFileAttrKind;
  11.  
  12. TFindFile = class(TObject)
  13. private
  14. s: TStringList;
  15.  
  16. fSubFolder : boolean;
  17. fAttr: TFileAttr;
  18. FPath : string;
  19. FBasePath: string;
  20. fFileMask : string;
  21. FDepth: integer;
  22.  
  23. procedure SetPath(Value: string);
  24. procedure FileSearch(const inPath : string);
  25. function cull(value: string): string;
  26. public
  27. constructor Create(path: string);
  28. destructor Destroy; override;
  29.  
  30. function SearchForFiles: TStringList;
  31.  
  32. property FileAttr: TFileAttr read fAttr write fAttr;
  33. property InSubFolders : boolean read fSubFolder write fSubFolder;
  34. property Path : string read fPath write SetPath;
  35. property FileMask : string read fFileMask write fFileMask ;
  36. end;
  37.  
  38. implementation
  39.  
  40. {$WARN SYMBOL_PLATFORM OFF}
  41. {$WARN UNIT_PLATFORM OFF}
  42. uses
  43. Windows, SysUtils, FileCtrl;
  44.  
  45. constructor TFindFile.Create(path: string);
  46. begin
  47. inherited Create;
  48. FPath := path;
  49. FBasePath := path;
  50. FileMask := '*.*';
  51. FileAttr := [ffaReadOnly, ffaDirectory, ffaArchive];
  52. s := TStringList.Create;
  53. FDepth := -1;
  54. end;
  55.  
  56. procedure TFindFile.SetPath(Value: string);
  57. begin
  58. if fPath <> Value then
  59. begin
  60. if (Value <> '') and (DirectoryExists(Value)) then
  61. fPath := IncludeTrailingPathDelimiter(Value);
  62. end;
  63. end;
  64.  
  65. function TFindFile.SearchForFiles: TStringList;
  66. begin
  67. s.Clear;
  68. try
  69. FileSearch(Path);
  70. finally
  71. Result := s;
  72. end;
  73. end;
  74.  
  75. function TFindFile.cull(value: string): string;
  76. begin
  77. result := StringReplace(value, FBasePath, '', []);
  78. end;
  79.  
  80. destructor TFindFile.Destroy;
  81. begin
  82. s.Free;
  83. inherited Destroy;
  84. end;
  85.  
  86. procedure TFindFile.FileSearch(const InPath : string);
  87. var Rec : TSearchRec;
  88. Attr : integer;
  89. begin
  90. inc(FDepth);
  91. try
  92. Attr := 0;
  93. if ffaReadOnly in FileAttr then
  94. Attr := Attr + faReadOnly;
  95. if ffaHidden in FileAttr then
  96. Attr := Attr + faHidden;
  97. if ffaSysFile in FileAttr then
  98. Attr := Attr + faSysFile;
  99. if ffaDirectory in FileAttr then
  100. Attr := Attr + faDirectory;
  101. if ffaArchive in FileAttr then
  102. Attr := Attr + faArchive;
  103. if ffaAnyFile in FileAttr then
  104. Attr := Attr + faAnyFile;
  105.  
  106. if SysUtils.FindFirst(inPath + FileMask, Attr, Rec) = 0 then
  107. try
  108. repeat
  109. if (Rec.Name = '.') or (Rec.Name = '..') then
  110. Continue;
  111. s.Add(cull(inPath) + Rec.Name);
  112. until SysUtils.FindNext(Rec) <> 0;
  113. finally
  114. SysUtils.FindClose(Rec);
  115. end;
  116.  
  117. If not InSubFolders then
  118. Exit;
  119.  
  120. if SysUtils.FindFirst(inPath + '*.*', faDirectory, Rec) = 0 then
  121. try
  122. repeat
  123. if ((Rec.Attr and faDirectory) <> 0) and (Rec.Name <> '.') and (Rec.Name <> '..') then
  124. begin
  125. FileSearch(IncludeTrailingPathDelimiter(inPath + Rec.Name));
  126. end;
  127. until SysUtils.FindNext(Rec) <> 0;
  128. finally
  129. SysUtils.FindClose(Rec);
  130. end;
  131. finally
  132. dec(FDepth);
  133. end;
  134. end;
  135.  
  136. end.
  137.  
  138. for i := 0 to High(FileNames) do begin ... end;
  139.  
  140. for i := 0 to Pred(FileNames.Count) do begin ... end;
  141.  
  142. for name in FileNames do begin ... end;
  143.  
  144. if FindFirst('*.txt', faAnyFile, SearchResult) = 0 then try
  145. repeat
  146. // Do something with SearchResult.Name
  147. until FindNext(SearchResult) <> 0;
  148. finally
  149. SysUtils.FindClose(SearchResult);
  150. end;
Add Comment
Please, Sign In to add comment