5

I am trying to populate a stringlist with all the folder names inside a directory.

Below is an extract of how I was able to do this:

var
  SL: TStringList;
  SearchAttr: LongInt;
  SR: TSearchRec;
begin
  SL := TStringList.Create;
  try
    SearchAttr := (faDirectory);

    if FindFirst(Directory + '\*.', SearchAttr, SR) = 0 then
    begin
      try
        repeat
          if (SR.Attr and faDirectory) <> 0 then
          begin
            if (SR.Name <> '.') and (SR.Name <> '..') then
            begin
              SL.Add(Directory + SR.Name);
            end;
          end;
        until
          FindNext(Sr) <> 0;
        finally
          FindClose(SR);
        end;
      end;
    end;

    // do something with string list folder names      
  finally
    SL.Free;
  end;
end;

The parent folder which I was accessing contains 220 sub folders but the routine was only adding 216 folder names. After some comparing and debugging I noticed the 4 folder names which were not been added contained dots in the names.

To test I created a folder called "Test Folder" and inside I added 9 more new folders named:

  • Folder 1
  • Folder 2
  • Folder 3
  • Folder 4
  • Folder 5
  • Folder .6
  • Folder 7
  • F.O.L.D.E.R 8
  • Folder 9

When using "Test Folder" as the parent directory, it only adds the following sub folders:

  • Folder 1
  • Folder 2
  • Folder 3
  • Folder 4
  • Folder 5
  • Folder 7
  • Folder 9

I have been experimenting with SR.Name <> '.', SR.Name <> '..' and SR.Name[1] <> '.' etc with no success.

How can I modify the code to allow folder names with dots in the name and add them to my stringlist?

Thanks

Craig
  • 1,874
  • 13
  • 41
  • 2
    Thanks mystery downvoter appreciate it, at least give a reason if you feel the need to downvote. – Craig Jul 02 '16 at 16:36
  • I don't understand the downvote. This is a well-written question that meets all of the requirements. – Ken White Jul 02 '16 at 23:43
  • @KenWhite thanks, fortunately I am not overly concerned about how many upvotes or downvotes I get, my main purpose of using this site is to learn and grow as a developer and help others when possible. I just find it quite annoying when mystery users take it upon themselves to downvote other users questions without even leaving a reason or anything constructive. If a users question is bad then downvoters should at least give a valid reason as to why and at least present an opportunity for the person to edit there question etc rather than be left thinking what they have done wrong. – Craig Jul 03 '16 at 12:33

1 Answers1

6

Change the search string from '*.' to '*'

Your search string only matches objects with an empty extension. You want to match all objects whether or not they have an extension.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
  • Thanks for spotting the mistake, I can't believe I missed that one, now I feel really stupid - I will just put it down as a bad day and move on ;) – Craig Jul 02 '16 at 16:38