Link to home
Start Free TrialLog in
Avatar of manney_mcvicker
manney_mcvicker

asked on

Show only ExtentionLESS files in TOpenDialog in Delphi

How do I create a file filter rule to use with TOpenDialog in Delphi that will only dispaly files that have no extention?
Avatar of developmentguru
developmentguru
Flag of United States of America image

 It looks like this will require creating a custom File Open Dialog.  The only two filters I got to show the extensionless file were *.* and * but they included all other files.

  If you create your own dialog you can use the Delphi functions to find the extensionless files and populate the control yourself.

  From what I can see there is no easy way to do this.
use  http://delphi.about.com/od/vclwriteenhance/a/tfindfile.html
for a own filedialog

uses FindFile;
...
procedure TfrMain.Button2Click(Sender:  TObject) ;
var FFile : TFindFile;
begin
  FFile :=  TFindFile.Create(nil) ;
  try
   FFile.FileAttr :=  [ffaAnyFile];
   FFile.InSubFolders := True;
   FFile.Path :=  ExtractFilePath(ParamStr(0)) ;
   FFIle.FileMask := '*.pas';

    Memo1.Lines := FFile.SearchForFiles;
  finally
   FFile.Free;
   end;
end;
not completly functiinal code, hope you can fix ...
unit findfile;
 
interface
 
uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, FileCtrl;
 
type
  TFileAttrKind = (ffaReadOnly, ffaHidden, ffaSysFile, ffaVolumeID, ffaDirectory, ffaArchive, ffaAnyFile);
  TFileAttr = set of TFileAttrKind;
 
  TFindFile = class(TComponent)
  private
    s : TStringList;
 
    fSubFolder : boolean;
    fAttr: TFileAttr;
    fPath : string;
    fFileMask : string;
 
    fnoExt   :  Boolean;
 
    procedure SetPath(Value: string);
    procedure FileSearch(const inPath : string);
    procedure DeleteFilesfromList;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
 
    function SearchForFiles: TStringList;
  published
    property FileAttr: TFileAttr read fAttr write fAttr;
    property InSubFolders : boolean read fSubFolder write fSubFolder;
    property Path : string read fPath write SetPath;
    property FileMask : string read fFileMask write fFileMask ;
  end;
 
procedure Register;
 
implementation
 
procedure Register;
begin
  RegisterComponents('About.com ', [TFindFile]);
end;
 
constructor TFindFile.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  Path := IncludeTrailingBackslash(GetCurrentDir); 
  FileMask := '*.*';
  FileAttr := [ffaAnyFile];
  s := TStringList.Create;
end;
 
destructor TFindFile.Destroy;
begin
  s.Free;
  inherited Destroy;
end;
 
procedure TFindFile.SetPath(Value: string);
begin
  if fPath <> Value then
  begin
    if Value <> '' then
      if DirectoryExists(Value) then
        fPath := IncludeTrailingBackslash(Value);
  end;
end;
 
procedure TFindFile.DeleteFilesfromList ;
var  i,j  :  Integer;
begin
  //  case of empty filemask delete all string with Pos('.',...) > 0
 
 if fNoExt then
  begin
 
  for i := 0 to s.Count-1 do
      if (( Pos('.', s[i])> 0) or ( Pos(':', s[i])> 0)) then s[i] :='';
 
 
  repeat
      j := s.IndexOf('');
 
      if j>0 then s.Delete(j);
 
  until  j= -1;
  end;
 
end;
 
 
function TFindFile.SearchForFiles: TStringList;
begin
  s.Clear;
  try
    FileSearch(Path);
  finally
    deleteFilesfromList;
    Result := s;
  end;
end;
 
procedure TFindFile.FileSearch(const InPath : string);
var Rec  : TSearchRec;
    Attr : integer;
    i,j  : integer;
begin
 
// case of empty file ext. look for all files ...
if (FileMask='') then begin
                      FileMask := '*.*';
                      fNoExt := true;
                      end
                      else
                      fNoExt := false;
 
Attr := 0;
if ffaReadOnly in FileAttr then Attr := Attr + faReadOnly;
if ffaHidden in FileAttr then Attr := Attr + faHidden;
if ffaSysFile in FileAttr then Attr := Attr + faSysFile;
if ffaVolumeID in FileAttr then Attr := Attr + faVolumeID;
if ffaDirectory in FileAttr then Attr := Attr + faDirectory;
if ffaArchive in FileAttr then Attr := Attr + faArchive;
if ffaAnyFile in FileAttr then Attr := Attr + faAnyFile;
 
if SysUtils.FindFirst(inPath + FileMask, Attr, Rec) = 0 then
 try
   repeat
     s.Add(inPath + Rec.Name);
   until SysUtils.FindNext(Rec) <> 0;
 finally
   SysUtils.FindClose(Rec);
 end;
 
If not InSubFolders then Exit;
 
if SysUtils.FindFirst(inPath + '*.*', faDirectory, Rec) = 0 then
 try
   repeat
   if ((Rec.Attr and faDirectory) <> 0)  and (Rec.Name<>'.') and (Rec.Name<>'..') then
     begin
       FileSearch(IncludeTrailingBackslash(inPath + Rec.Name));
     end;
   until SysUtils.FindNext(Rec) <> 0;
 finally
   SysUtils.FindClose(Rec);
 end;
 
 
 
 
 
 
 
end;
 
 
 
end.
 
{
********************************************
Zarko Gajic
About.com Guide to Delphi Programming
http://delphi.about.com
email: delphi.guide@about.com
free newsletter: http://delphi.about.com/library/blnewsletter.htm
forum: http://forums.about.com/ab-delphi/start/
********************************************
}

Open in new window

SOLUTION
Avatar of Eddie Shipman
Eddie Shipman
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
One other way would be to get the handle to the ListView in the OpenDialog and removing the items that have an extension, yourself, but that is much harder work.
Avatar of manney_mcvicker
manney_mcvicker

ASKER

Thanks for all your posts.

I was going to create my own custom open dialog originally, but was hoping someone had a clever solution that derived from the base dialog class TOpenDialog
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial