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
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.