4

I typically use TOpenDialog with its Filter property for narrowing down available files for the user. I would like to further filter these files down by their content.

For example, the open file dialog should display only files with a ".bin" filename extension and its first 4 magic bytes are 0x7F, 0x45, 0x4C, 0x46 (ELF executable).

The condition will not always necessarily be identifying certain executable formats, but other binary data as well.

Is there a standard way of doing this in Delphi/VCL or do I have to postpone the content inspection until after the user has selected the file?

ardnew
  • 2,028
  • 20
  • 29
  • I would be tempted to use some old components to create my own dialog: Mustang Peak VirtualShellTools (https://code.google.com/p/mustangpeakvirtualshelltools/) or Embarcadero's version (http://sourceforge.net/projects/embtvstools/) with TFindFile (http://www.delphiarea.com/products/delphi-components/findfile/) to filter on content. See http://knowisinc.com/wpdname.html for an example. – Max Williams Mar 12 '14 at 07:57
  • I was locked out of finishing my edit. Anyway, WPDName identifies old Word Perfect files by checking the first three bytes of each file. I will give you the source if you want. – Max Williams Mar 12 '14 at 08:06

1 Answers1

6

There's no way to achieve what you want. The system file dialog does not allow you to filter the files that it displays based on their content.

The OnIncludeItem event tantalisingly appears to do what you need. However, it has the following problems.

  1. The event wraps the CDN_INCLUDEITEM notification. This notification is fired by the legacy XP common dialog boxes, but not by the modern common item dialog. So, using the event forces the legacy XP common dialog boxes onto your program. If for no other reason, this has cosmetic downsides.
  2. More significantly, handling CDN_INCLUDEITEM has no impact on files. It only influences non-filesystem shell objects. Form the documentation:

The dialog box always includes items that have both the SFGAO_FILESYSTEM and SFGAO_FILESYSANCESTOR attributes, regardless of the value returned by CDN_INCLUDEITEM.

Related questions:

Community
  • 1
  • 1
David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • Turns out you were right. (Tried to remove my answer earlier, but the read-only prevented it. Had to wait.) Thanks. – Ken White Mar 11 '14 at 16:31
  • @Ken NP. You had doubting for a while. – David Heffernan Mar 11 '14 at 17:30
  • Turned out it was my mistake. The first *.bat files in my test folder (coincidentally) matched the test filter (Pos('test', FileName) > 0), so what was visible in the dialog below the folders appeared to be properly filtered. When I read your answer, I went back to double-check and realized the error, came back to delete the answer, and couldn't do so. :-) – Ken White Mar 11 '14 at 19:07