Forum > Beginners
DirToListbox Questions(Solved)
JLWest:
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 [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TForm1.DirToListbox(Const APath : String; Const aBox : TListbox); VAR Info : TSearchRec; begin If FindFirst (aPath + '*',faAnyFile,Info)=0 then begin repeat with Info do begin if (Attr and faAnyFile) <> 0 then // If we find a file if Name = '.' then begin Continue; end; if Name = '..' then begin Continue; end; abox.Items.Add(Name) end until FindNext(info)<>0; end; FindClose(Info); end;
Thanks
And the answer is 'faDirectory' DUH
simone:
You have to fix the code as follows:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---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
KodeZwerg:
You can also take a look in unit FileUtil and use findalldirectories or findallfiles O:-)
JLWest:
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.
KodeZwerg:
I do not understand your problem, this a possibility of how you could use the FileUtil methods.
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---uses .... FileUtil .... procedure TForm1.Button1Click(Sender: TObject);var i: Integer; LList: TStringList;begin ListBox1.Items.BeginUpdate; ListBox1.Clear; LList := FindAllDirectories(ExtractFilePath(ParamStr(0)), False, ';'); try for i := 0 to Pred(LList.Count) do ListBox1.Items.Add(LList.Strings[i]); finally LList.Free; end; LList := FindAllFiles(ExtractFilePath(ParamStr(0)), '*.*', False, faDirectory, ';', ';'); try for i := 0 to Pred(LList.Count) do ListBox1.Items.Add(LList.Strings[i]); finally LList.Free; end; ListBox1.Items.EndUpdate;end;
Navigation
[0] Message Index
[#] Next page