Recent

Author Topic: DirToListbox Questions(Solved)  (Read 568 times)

JLWest

  • Hero Member
  • *****
  • Posts: 1281
DirToListbox Questions(Solved)
« on: December 29, 2022, 04:01:13 am »
I have the following procedures to load files that are in a directory into a listbox.

I believe the Parm faAnyFile determines what is loaded into the list box.
I would like to be able to load all files which is '*' or '*.*' and/or just sub directories in a directory.
It would be something like faDir but I haven't been able to make it work.

Or maybe I'm wrong and there is any such fa to do that.


Code: Pascal  [Select][+][-]
  1. procedure TForm1.DirToListbox(Const APath : String; Const aBox : TListbox);
  2.  VAR  Info : TSearchRec;
  3.  begin
  4.   If FindFirst (aPath + '*',faAnyFile,Info)=0 then begin
  5.     repeat
  6.       with Info do begin
  7.         if (Attr and faAnyFile) <> 0 then     // If we find a file
  8.            if Name =  '.' then begin Continue; end;
  9.            if Name = '..'  then begin Continue; end;
  10.            abox.Items.Add(Name)
  11.       end
  12.       until FindNext(info)<>0;
  13.     end;
  14.     FindClose(Info);
  15.  end;          

Thanks

And the answer is 'faDirectory' DUH
« Last Edit: December 29, 2022, 04:55:34 am by JLWest »
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

simone

  • Hero Member
  • *****
  • Posts: 505
Re: DirToListbox Questions(Solved)
« Reply #1 on: December 29, 2022, 02:54:48 pm »
You have to fix the code as follows:

Code: Pascal  [Select][+][-]
  1. If FindFirst (IncludeTrailingPathDelimiter(aPath) + '*',faAnyFile,Info)=0 then begin

IncludeTrailingPathDelimiter ensures that there is a DirectorySeparator at the end of apath. Otherwise, since only files whose names match the pattern: string apath + * are found, you lose all the contents of the folder.


For information about IncludeTrailingPathDelimiter, see:

https://www.freepascal.org/docs-html/rtl/sysutils/includetrailingpathdelimiter.html

Microsoft Windows 10 64 bit - Lazarus 2.2.4

KodeZwerg

  • Hero Member
  • *****
  • Posts: 1187
  • Fifty shades of code.
    • Delphi & FreePascal
Re: DirToListbox Questions(Solved)
« Reply #2 on: December 29, 2022, 03:15:24 pm »
You can also take a look in unit FileUtil and use findalldirectories or findallfiles  O:-)
« Last Edit: Tomorrow at 31:76:97 by KodeZwerg »

JLWest

  • Hero Member
  • *****
  • Posts: 1281
Re: DirToListbox Questions(Solved)
« Reply #3 on: December 29, 2022, 04:55:57 pm »
What I'm after is the sub directory name. I'll have tp play with this. I think it will be difficult to get it to work on both Windows and unix.
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

KodeZwerg

  • Hero Member
  • *****
  • Posts: 1187
  • Fifty shades of code.
    • Delphi & FreePascal
Re: DirToListbox Questions(Solved)
« Reply #4 on: December 29, 2022, 06:00:06 pm »
I do not understand your problem, this a possibility of how you could use the FileUtil methods.
Code: Pascal  [Select][+][-]
  1. uses
  2.   .... FileUtil ....
  3.  
  4.  
  5. procedure TForm1.Button1Click(Sender: TObject);
  6. var
  7.   i: Integer;
  8.   LList: TStringList;
  9. begin
  10.   ListBox1.Items.BeginUpdate;
  11.   ListBox1.Clear;
  12.   LList := FindAllDirectories(ExtractFilePath(ParamStr(0)), False, ';');
  13.   try
  14.     for i := 0 to Pred(LList.Count) do
  15.       ListBox1.Items.Add(LList.Strings[i]);
  16.   finally
  17.     LList.Free;
  18.   end;
  19.   LList := FindAllFiles(ExtractFilePath(ParamStr(0)), '*.*', False, faDirectory, ';', ';');
  20.   try
  21.     for i := 0 to Pred(LList.Count) do
  22.       ListBox1.Items.Add(LList.Strings[i]);
  23.   finally
  24.     LList.Free;
  25.   end;
  26.   ListBox1.Items.EndUpdate;
  27. end;
« Last Edit: December 29, 2022, 06:14:31 pm by KodeZwerg »
« Last Edit: Tomorrow at 31:76:97 by KodeZwerg »

JLWest

  • Hero Member
  • *****
  • Posts: 1281
Re: DirToListbox Questions(Solved)
« Reply #5 on: December 29, 2022, 06:34:54 pm »
@KodeZwerg Thank you for the code example.

I don't know that I had a problem. Trying to develop a program(s) that will run on Win and Unix. And I don't have Unix I'm never really sure when I'm coding involving paths/directories the code will work on Unix.

I have a requirement to get a listing of all the '.txt' files in a directory. And a  separate requirement to get a list of all the sub directories in a directory.

Looking at your code example it looks like it will do that.
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

Bart

  • Hero Member
  • *****
  • Posts: 4934
    • Bart en Mariska's Webstek
Re: DirToListbox Questions(Solved)
« Reply #6 on: December 29, 2022, 06:42:50 pm »
Why not use a TFileListBox? It's made for that.

Bart

JLWest

  • Hero Member
  • *****
  • Posts: 1281
Re: DirToListbox Questions(Solved)
« Reply #7 on: December 30, 2022, 05:05:01 am »
@wp

I didn't know about a TFileListbox. Will have to do a demo and see what it's about.
Thanks.
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

 

TinyPortal © 2005-2018