-1

I am new to Delphi. I have developed a VBA program which starts by reading in many small text files into an array using the code below. I am just starting to rewrite the program in Delphi 10.3.

Is there an equivalent code in Delphi? or

Suggestions welcome.

Set fso = CreateObject("scripting.filesystemobject")
Set MyFolder = fso.GetFolder(FileNameSource)
Set MyFileNames = MyFolder.Files

AllCount = MyFolder.Files.Count

ReDim AllMyWeeklyFileContents(1 To AllCount) As string
AllCount = 1
For Each MyFiles In MyFileNames
  AFileName = MyFolder & "\" & MyFiles.Name
  Set MyFileToRead = fso.GetFile(AFileName)
  Set MyFileContent = MyFileToRead.OpenAsTextStream(ForReading, TristateUseDefault)
  AllMyWeeklyFileContents(AllCount) = MyFileContent.ReadAll
  AllCount = AllCount + 1
Next
Set fso = Nothing
Set MyFolder = Nothing
Progman
  • 16,827
  • 6
  • 33
  • 48
  • 1
    I am not a Delphi expert, so I probably cannot help you. But I guess you will need to add your own Delphi rewrite attempt(s) in your question. That allows us to provide more focused/detailed suggestions to you. – Bart Hofland Mar 14 '21 at 09:46
  • 3
    (1) Declare an array of string with the same length as your file name array. (2) Iterate over it. (3) For each file, read it using `TFile.ReadAllText` from `IOUtils`. – Andreas Rejbrand Mar 14 '21 at 10:02
  • 4
    If you are new to Delphi, I suggest you learn about the language and the libraries. – David Heffernan Mar 14 '21 at 12:06

2 Answers2

3

Below is the equivalent written in Delphi. To use the code, create a new Windows VCL application, on the form, drop a button and a memo and use the code below for the button OnClick event handler.

The code get the file list in a given folder, read each file, concatenating the lines in an array. Then iterate the array to show in the memo.

unit ReadManyFilesDemoMain;

interface

uses
    Winapi.Windows, Winapi.Messages,
    System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
    System.IOUtils, System.Types,
    System.Generics.Collections, System.Generics.Defaults,
    Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;

type
    TForm1 = class(TForm)
        Button1: TButton;
        Memo1: TMemo;
        procedure Button1Click(Sender: TObject);
    end;

var
    Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
    FileNames : TArray<String>;   // The array for all filenames
    FileName  : String;           // A single filename
    Lines     : TArray<String>;   // The array of all lines
    Line      : String;           // A single line
begin
    // Get all text file names from given path
    FileNames := TDirectory.GetFiles('E:\Temp', '*.txt');
    // For each filename, add his lines to the dynamic array
    for FileName in FileNames do
        Lines := TArray.Concat<String>([Lines, TFile.ReadAllLines(FileName)]);
    // Display in the memo all collected lines
    for Line in Lines do
        Memo1.Lines.Add(Line);
end;

end.
fpiette
  • 11,983
  • 1
  • 24
  • 46
1

Basically, there are multiple questions:

  • how to get list of (text)files in a specific folder (probably with a specific file mask...)
  • how to create a dynamic (string) array to put the content of all files in
  • how to read all the content of text files

The first

Files := TDirectory.GetFiles(FolderPath, FileMask);

The second

SetLength(FileContent, Length(Files));

The last one (base on Andreas comment)

FileContent := TFile.ReadAllText(Filename)

When put together in a function:

function GetAllTextFilesContent(const FolderPath, FileMask: string): TArray<string>;
var
  I: integer;
  Files: TArray<string>;
begin
  // list files
  Files := TDirectory.GetFiles(FolderPath, FileMask);
  // set size result array to number of files
  SetLength(Result, Length(Files));
  // read and put file content of each file into the result array
  for I := 0 to Length(Files) - 1 do
    Result[I] := TFile.ReadAllText(Files[I]);
end;

Please note: this routine does not check whether the files are actually text files. When binary files are part of the file mask, you might get some strange results!

R. Hoek
  • 916
  • 8
  • 27