Board index » delphi » files and dirs

files and dirs

Hi,

I'm making a kind of Explorer and i need to know when i'm listing a file or
when i'm listing a directory, because there are also files without
extensions the simplest way is out of the question.(simplest way = if the
file has no extension it must be a directory)

How is it done, via the FileInfo or something

Please help,

Vincent.

 

Re:files and dirs


Quote
Vijt Vincent <vi...@belgacom.net> wrote in message

news:8qldvc$4qn$1@news1.skynet.be...

Quote
> Hi,

> I'm making a kind of Explorer and i need to know when i'm listing a
file or
> when i'm listing a directory, because there are also files without
> extensions the simplest way is out of the question.(simplest way = if
the
> file has no extension it must be a directory)

> How is it done, via the FileInfo or something

> Please help,

> Vincent.

Hi,

When using FindFirst() and FindNext() you can check the TSearchRec.Attr.

if TSearchRec.Attr AND faDirectory > 0 then {this is a directory}

HTH
-ThomasN

Re:files and dirs


Vijt Vincent <vi...@belgacom.net> schreef in berichtnieuws
8qldvc$4q...@news1.skynet.be...

Quote
> Hi,

> I'm making a kind of Explorer and i need to know when i'm listing a file
or
> when i'm listing a directory, because there are also files without
> extensions the simplest way is out of the question.(simplest way = if the
> file has no extension it must be a directory)

> How is it done, via the FileInfo or something

> Please help,

> Vincent.

Oops...Note that a directory _can_ have an extension, even multiple
extensions (e.g. c:\temp\xyz.abc.hjk)

Anyway, see FindFirst/FindNext and TSearchRec. The field Attr contains
information about the file attributes as follows :

faReadOnly  $00000001 Read-only files
faHidden    $00000002 Hidden files
faSysFile   $00000004 System files
faVolumeID  $00000008 Volume ID files
faDirectory $00000010 Directory files
faArchive   $00000020 Archive files
faAnyFile   $0000003F Any file

Below is an example of how to enumerate files, given a wildcard, e.g.
'd:\temp\*.txt';
Note that in the example, dirs are _excluded_ , so you will have to change
the code!

--
Regards,

Dirk Claessens
---------------------------------------------------------
http://www.claessens16.yucom.be
Attention: All spamshields raised; E-mails will bounce!
---------------------------------------------------------

procedure EnumFiles( WildCard: string; FileList: TStrings; stripExtensions:
boolean );
var
 SRec : TSearchRec;
 Error : DWORD;
begin
 try
  FileList.Clear;
  Error := FindFirst( WildCard, faANYFILE, SRec);
  while Error = 0 do
  begin
    if SRec.Attr <> faDIRECTORY then
      if not stripExtensions then
           FileList.Add( SRec.Name )
      else
           FileList.Add( ChangeFileExt(SRec.Name, '') );
    Error := FindNext( SRec);
  end;
  Sysutils.FindClose(SRec);
 except
  messagebeep(0);
 end;
end;

Re:files and dirs


The Delphi help tells to use this method (assuming you iterate through
items in a directory with TSearchRec):

  if (SearchRec.Attr and faDirectory) > 0 then
    {it is a directory}
  else
    {it is not a directory}

Also, you can completely discard any directory- or other type items from
the list by subtracting the attributes in FindFirst() function's
parameter:

  faAnyFile - faDirectory

or

  faAnyFile - faHidden

etc.

Even more, every directory (not root directories?) have two directories
in them, that are usually invisible to users in file managers for
convenience reasons. These are:

  .. , which means the parent directory
  .  , which means the directory itself

You should check for the existence of these in your loop and do the
appropriate for them. Best to discard "." from your file list
completely.

Here is a simple loop, clean code.
You should put up a subproc to add an item, for it to look nicer in the
coder's eye, maybe you should put it in a separate thread, etc...

---

procedure GetDirList(Dir: string);
var
  SR  : TSearchRec;
  Rec : word;
begin

  if not DirectoryExists(Dir) then begin
    {dir doesn't exist; handle the error somehow}
    Exit;
  end;

  Rec := FindFirst( Dir + '\*.*', faAnyFile, SR );

  try

    while Rec = 0 do begin

      if SR.Name = '.' then

        {"dummy" directory; do nothing}

      else if SR.Name = '..' then

        {"parent dir" directory - put a Level Up-icon}

      else if (SR.Attr and faDirectory) > 0 then

        {the item is a directory; put a dir icon for it}

      else

        {a file is in question; put a file icon}

      Rec := FindNext(SR);

    end;

  finally
    FindClose(SR);
  end;

end;

--------------------------------

Quote
Thomas Nelvik wrote:

> Vijt Vincent <vi...@belgacom.net> wrote in message
> news:8qldvc$4qn$1@news1.skynet.be...
> > Hi,

> > I'm making a kind of Explorer and i need to know when i'm listing a
> file or
> > when i'm listing a directory, because there are also files without
> > extensions the simplest way is out of the question.(simplest way = if
> the
> > file has no extension it must be a directory)

> > How is it done, via the FileInfo or something

> > Please help,

> > Vincent.

> Hi,

> When using FindFirst() and FindNext() you can check the TSearchRec.Attr.

> if TSearchRec.Attr AND faDirectory > 0 then {this is a directory}

> HTH
> -ThomasN

Re:files and dirs


I can see the header of your post on my ISP's copy of the
borland.public.delphi.oleautomation newsgroup,
but it hasn't been sent to Borland's news server.
That means that most of the people who read
Borland's newsgroups cannot see your post.

To post messages that other people will see, you
need to connect to Borland's servers directly:
either newsgroups.borland.com or forums.borland.com.
Here's a link with instructions if you don't know how:
http://www.borland.com/newsgroups/newsinfo.html

If you cannot connect directly to newsgroups because
of a firewall, you can post to Borland's newsgroups
via the web pages at http://newsgroups.borland.com/

--
Deborah

Other Threads