FindFirst/FindNext and faDirectory (how to populate a treeeview from directory) lazarus / 32-Linux ---------------------------------------------------------------------- Max JRB · 2012-10-12 23:29 Doing a loop to populate a Treeview from a source directory. I found, that on windows the faDirectory value is 16, on Unix(LINUX/OsX) this value is not 16 for directories, this value is 48, I don't know if this is a bug, but I thought that faDirectory should give you the right value for each OS. Here is the code: Code: [Select]procedure TfrmMain.btnAgregaMarcadorClick(Sender: TObject); var     lcSearch: TSearchRec;     lcFileAttr: integer;     lcFatherNode: TTreeNode;     lcChildNode: TTreeNode;     lcPath: String; begin     {$IFDEF UNIX}         lcFileAttr:= 48;     {$ELSE}         lcFileAttr:= faDirectory;     {$ENDIF}     SelectDirectoryDialog1.Execute;     lcPath:=SelectDirectoryDialog1.FileName;     if Length(lcPath) > 0 then begin         lcFatherNode:= TTreeNode.Create(TreeView1.Items);         lcFatherNode:= TreeView1.Items.AddChild(TreeNodeRoot,lcPath);         lcFatherNode.ImageIndex:=0;         lcFatherNode.SelectedIndex:=0;         lcFatherNode.Expanded:=True;     end;     lcPath:=lcPath+PathDelim;     If FindFirst(lcPath+'*',faDirectory,lcSearch)=0 then begin         repeat             if lcSearch.Attr = lcFileAttr then begin                 if (lcSearch.Name = '.') or (lcSearch.Name = '..') then continue;                 lcChildNode:= TreeView1.Items.AddChild(lcFatherNode,lcSearch.Name);                 lcChildNode.ImageIndex:=0;                 lcChildNode.SelectedIndex:=0;                 AddDirectorios(lcChildNode,lcPath+lcSearch.Name);             end;         until FindNext(lcSearch) <> 0;         FindClose(lcSearch);     end;    TreeView1.Refresh; end;       This is the procedure "AddDirectorios" Code: [Select]procedure TfrmMain.AddDirectorios(elNodo: TTreeNode; cPath: String); var     sr: TSearchRec;     lcFileAttr: Integer;     theNewNode : tTreeNode; begin     {$IFDEF UNIX}         lcFileAttr:= 48;     {$ELSE}         lcFileAttr:= faDirectory;     {$ENDIF}     if FindFirst(cPath+PathDelim+'*', faDirectory, sr) = 0 then begin         repeat             if sr.Attr = lcFileAttr then begin                 if (sr.Name = '.') or (sr.Name = '..') then continue;                     theNewNode := TreeView1.Items.AddChild(elNodo,sr.name);                     theNewNode.ImageIndex:=0;                     theNewNode.SelectedIndex:=0;                     AddDirectorios(theNewNode,cPath+PathDelim+sr.Name);             end;         until FindNext(sr) <> 0;         FindClose(sr);     end; end; ---------------------------------------------------------------------- JuhaManninen · 2012-10-13 00:53 The Attr is a bit field. You must do a bit-wise AND to get the right info. Like this: Code: [Select]if (sr.Attr and faDirectory) = faDirectory then ... It can also be found easily by googling. Regards, Juha