delphidwscript

How to use dwsXPlatform.CollectFiles TCollectFileProgressEvent


As I am new to events in delphi I am struggling on how to use dwsXPlatform.TCollectFileProgressEvent in conjunction with dwsXPlatform.CollectFiles.

In the DWScript repository there is no sample or even test code for it.

type
        TForm1 = class(TForm)
                btn1: TButton;
                mmoDirList: TMemo;
                mmoOnCollectFiles: TMemo;
                procedure btn1Click(Sender: TObject);
        private
                OnCollectFileProgressEvent: TCollectFileProgressEvent;
        end;
{...}
procedure TForm1.btn1Click(Sender: TObject);
begin
        mmoDirList.Clear;
        CollectFiles('c:\MyDelphiFiles', '*.pas', mmoDirList.Lines, True, OnCollectFileProgressEvent);
end;

Solution

  • [SOLVED]

    unit MainFormU;
    
    interface
    
    uses
            Winapi.Windows,
            Winapi.Messages,
            System.SysUtils,
            System.Variants,
            System.Classes,
            Vcl.Graphics,
            Vcl.Controls,
            Vcl.Forms,
            Vcl.Dialogs,
            Vcl.StdCtrls,
            dwsXPlatform;
    
    type
            TForm1 = class(TForm)
                    btn1: TButton;
                    mmoDirList: TMemo;
                    mmoOnCollectFiles: TMemo;
        chkEnableOnCollectEvent: TCheckBox;
                    procedure btn1Click(Sender: TObject);
                    procedure OnCollectFileProgressEvent(const aDirectory: string; var aSkipScan: Boolean);
            private
                    FOnCollectFiles: TCollectFileProgressEvent;
            end;
    
    var
            Form1: TForm1;
    
    implementation
    
    {$R *.dfm}
    
    procedure TForm1.btn1Click(Sender: TObject);
    begin
            mmoDirList.Clear;
            mmoOnCollectFiles.Clear;
    
            if chkEnableOnCollectEvent.Checked then
                    FOnCollectFiles := OnCollectFileProgressEvent
            else
                    FOnCollectFiles := nil;
    
            // procedure CollectFiles(const directory: UnicodeString;
            //                              fileMask: UnicodeString;
            //                              list: TStrings;
            //                              recurseSubdirectories: Boolean = False;
            //                              onProgress: TCollectFileProgressEvent = nil);
    
            CollectFiles('c:\MyFolder\', '*.pas', mmoDirList.Lines, True, FOnCollectFiles);
    end;
    
    procedure TForm1.OnCollectFileProgressEvent(const aDirectory: string; var aSkipScan: Boolean);
    begin
            if aDirectory = 'c:\MyFolder\SkipThisFolder\' then begin
                    ShowMessage('Folder ' + aDirectory + ' was skipped!');
                    aSkipScan := True;
            end;
    
            mmoOnCollectFiles.Lines.Add(aDirectory);
    end;
    
    end.