Forum > Beginners

[Solved] How to search recursively using TDirectory.GetFiles

(1/1)

ikel:
I have a sub-folder called sub-folder. In the sub-folder there are other sub folders.
When I run
--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---searchResults := TDirectory.GetFiles('./sub-folder/', '*.csv', TSearchOption.soAllDirectories);, it fails to search files inside ./sub-folder/csv/

How do I use TDirectory.GetFiles so I can look for files recursively? I thought TSearchOption.soAllDirectories would search recursively .


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program IOUtilsListAllFiles; {$mode objfpc}{$H+}{$J-} uses  {$IFDEF UNIX}  cthreads,  {$ENDIF}  Classes,  System.IOUtils,  Types,  SysUtils; var  searchResults: TStringDynArray;  item: string; begin   // Call TDirectory.GetFiles  searchResults := TDirectory.GetFiles('./sub-folder/', '*.csv', TSearchOption.soAllDirectories);   if Length(searchResults) > 0 then  begin    for item in searchResults do      WriteLn(item);  end  else  begin    WriteLn('Not found');  end;   // Pause console  WriteLn;  WriteLn('Press Enter key to exit ...');  ReadLn;end.

ASerge:
It looks like a Delphi code.
In FPC:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program IOUtilsListAllFiles; {$mode objfpc}{$H+}{$J-}{$APPTYPE CONSOLE} uses  {$IFDEF UNIX}  cthreads,  {$ENDIF}  Classes,  FileUtil,  SysUtils; var  SearchResults: TStringList;  Item: string;begin  SearchResults := FindAllFiles('./sub-folder/', '*.csv', True);  try    if SearchResults.Count > 0 then      for Item in SearchResults do        WriteLn(Item)    else      WriteLn('Not found');  finally    SearchResults.Free;  end;  // Pause console  WriteLn;  WriteLn('Press Enter key to exit ...');  ReadLn;end.

ikel:
Hi ASerge,

`FileUtils` from `LazUtils` works well. I'll stick with `FindAllFiles` for now.

I was just testing TDirectory as shown here; https://www.freepascal.org/daily/packages/vcl-compat/system.ioutils/tdirectory.html

Thank you!

Navigation

[0] Message Index

Go to full version