user2153076
user2153076

Reputation: 11

How to filter TShellListView items to a specific file extension?

I'm working on a project in Delphi 7 that needs ShellListView1 so show .PNG or .JPG files only.

How can I view only folders and specific file types (example: '.exe;.bat') ?

I was told is a ShellListView1 component with masking but websites I try are offline.

Upvotes: 1

Views: 2310

Answers (2)

Andrei Krasutski
Andrei Krasutski

Reputation: 5267

uses Masks;
...
procedure TForm1.ShellListView1AddFolder(Sender: TObject;
  AFolder: TShellFolder; var CanAdd: Boolean);
begin
  CanAdd := AFolder.IsFolder or MatchesMask(AFolder.PathName, '*.exe');
end;

function MatchesMask() returns True is a string value matches a format specifed by a mask.

Syntactically valid Mask consists of literal characters, sets, and wildcards. Wildcards are asterisks (*) or question marks (?). An asterisk matches any number of characters. A question mark matches a single arbitrary character.

Upvotes: 1

TLama
TLama

Reputation: 76713

You can write a handler for the OnAddFolder event, which fires whenever an item is going to be added to the list. The following code allows to add only files with *.exe or *.bat extension to the list:

procedure TForm1.ShellListView1AddFolder(Sender: TObject;
  AFolder: TShellFolder; var CanAdd: Boolean);
var
  FileExt: string;
begin
  CanAdd := not AFolder.IsFolder;
  if CanAdd then
  begin
    FileExt := ExtractFileExt(AFolder.PathName);
    CanAdd := (FileExt = '.exe') or (FileExt = '.bat');
  end;
end;

Upvotes: 5

Related Questions