1

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;
Fabio Vitale
  • 2,257
  • 5
  • 28
  • 38

2 Answers2

1

According to the documentation TCollectFileProgressEvent is dseclared as following:

TCollectFileProgressEvent = procedure (const directory : String; var skipScan : Boolean) of object;

Let's split that into 3 pieces:

1) TCollectFileProgressEvent

2) procedure (const directory : String; var skipScan : Boolean)

3) of object

The first part TCollectFileProgressEvent is the name of the event type. You don't need that for any thing in your example.

The second part procedure .... Is a recipe of how you should declare the event

The third part "of object" means that your procedure needs to be placed on a class.

Let me show you some code:

  TForm1 = class(TForm)
    btn1: TButton;
    mmoDirList: TMemo;
    mmoOnCollectFiles: TMemo;
    procedure btn1Click(Sender: TObject);
  private
    procedure CollectFileProgress(const directory : String; var skipScan : Boolean);
  end;


{ TForm1 }

procedure TForm1.btn1Click(Sender: TObject);
begin
  mmoDirList.Clear;
  CollectFiles('c:\MyDelphiFiles', '*.pas', mmoDirList.Lines, True, CollectFileProgress);
end;

procedure TForm1.CollectFileProgress(const directory: String; var skipScan: Boolean);
begin
  mmoDirList.Lines.Add(directory);
end;
Jens Borrisholt
  • 6,174
  • 1
  • 33
  • 67
  • 1
    Thank you +Jens can you please point me to the documentation? – Fabio Vitale Dec 06 '17 at 18:24
  • Not realy documentation but I googled the event and found the source code online https://github.com/EricGrange/DWScript/blob/master/Source/dwsXPlatform.pas By the way you don't need DWScript to do this you can just use the build in functionality – Jens Borrisholt Dec 07 '17 at 10:29
0

[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.
Fabio Vitale
  • 2,257
  • 5
  • 28
  • 38