Recent

Author Topic: Find Directories in path and put in TStringList  (Read 3877 times)

af0815

  • Hero Member
  • *****
  • Posts: 1409
Re: Find Directories in path and put in TStringList
« Reply #15 on: February 26, 2022, 03:51:43 pm »
TDirList.Count gives you back how much strings are in the list. Use this first to look if something is in the list.
regards
Andreas

jamie

  • Hero Member
  • *****
  • Posts: 7693
Re: Find Directories in path and put in TStringList
« Reply #16 on: February 26, 2022, 04:40:29 pm »
TDirList.Count gives you back how much strings are in the list. Use this first to look if something is in the list.

May I ask where that is located in the standard libs with the Lazarus or Fpc install?

enquiring minds would like to know!  :o
The only true wisdom is knowing you know nothing

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Find Directories in path and put in TStringList
« Reply #17 on: February 26, 2022, 04:53:10 pm »
The following little program uses the FindAllDirectories routine. You need to add LCLBase as a dependency to the project.
Code: Pascal  [Select][+][-]
  1. program Project1;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   {$IFDEF UNIX}
  7.   cthreads,
  8.   {$ENDIF}
  9.   Classes, SysUtils,
  10.   FileUtil, LazLogger;
  11.  
  12. function FoundFoldersInPathOK(aPath: String; aList: TStringList; aSearchSubDirs: Boolean=True): Boolean;
  13. begin
  14.   Result := False;
  15.   if not Assigned(aList) or (aPath = '') then Exit;
  16.   aList.Clear;
  17.   FindAllDirectories(aList, aPath, aSearchSubDirs);
  18.   Result := aList.Count > 0;
  19. end;
  20.  
  21. var
  22.   folderList: TStringList;
  23.   s: String;
  24.  
  25. begin
  26.   folderList := TStringList.Create;
  27.   try
  28.     case FoundFoldersInPathOK(GetCurrentDir, folderList) of
  29.       True: for s in folderList do
  30.               DebugLn(s);
  31.       False: DebugLn([GetCurrentDir,' has no sub directories']);
  32.     end;
  33.   finally
  34.     folderList.Free;
  35.   end;
  36. end.

af0815

  • Hero Member
  • *****
  • Posts: 1409
Re: Find Directories in path and put in TStringList
« Reply #18 on: February 26, 2022, 05:07:40 pm »
TDirList.Count gives you back how much strings are in the list. Use this first to look if something is in the list.

May I ask where that is located in the standard libs with the Lazarus or Fpc install?

enquiring minds would like to know!  :o

This is the Header of the Thread "Re: Find Directories in path and put in TStringList"

So the TDirList is a misspelled TStringList. (And correct namend should be DirList).

BTW: Look in the post of nugax
Quote from: nugax
  F: textfile;
  sFileName : string;
  TDirList : TStringList;
  iNumbdir : integer;

regards
Andreas

nugax

  • Full Member
  • ***
  • Posts: 232
Re: Find Directories in path and put in TStringList
« Reply #19 on: February 26, 2022, 06:33:51 pm »
I have no idea why all these functions find nothign in the location: Here is the code:


Code: Pascal  [Select][+][-]
  1. FindAllFiles(TDirList, programinfo.cbbsInfo.sSessionsDir, '*', true);  

https://wiki.freepascal.org/FindAllFiles


output:

CMD->: Number: 0Folder: /home/cadams/git/cyberbbs/build/tmp/sessions/ 
  Number: 0Folder: /home/cadams/git/cyberbbs/build/tmp/sessions/


That won't do?
-Nugax

nugax

  • Full Member
  • ***
  • Posts: 232
Re: Find Directories in path and put in TStringList
« Reply #20 on: February 26, 2022, 06:36:31 pm »
TDirList.Count gives you back how much strings are in the list. Use this first to look if something is in the list.

May I ask where that is located in the standard libs with the Lazarus or Fpc install?

enquiring minds would like to know!  :o

This is the Header of the Thread "Re: Find Directories in path and put in TStringList"

So the TDirList is a misspelled TStringList. (And correct namend should be DirList).

BTW: Look in the post of nugax
Quote from: nugax
  F: textfile;
  sFileName : string;
  TDirList : TStringList;
  iNumbdir : integer;

Why does the name of the variable have anything to do with the code working or not working? TDirList isnt reserved for anything else, although I agree it may be best to just name it DirList.
-Nugax

nugax

  • Full Member
  • ***
  • Posts: 232
Re: Find Directories in path and put in TStringList
« Reply #21 on: February 26, 2022, 06:38:28 pm »
I changed the name of the variable, and it changed nothing. Still doesnt find the directories. There are 2 directories in the path.

There has to be something incorrect in the function calls..



TDirList.Count gives you back how much strings are in the list. Use this first to look if something is in the list.

May I ask where that is located in the standard libs with the Lazarus or Fpc install?

enquiring minds would like to know!  :o

This is the Header of the Thread "Re: Find Directories in path and put in TStringList"

So the TDirList is a misspelled TStringList. (And correct namend should be DirList).

BTW: Look in the post of nugax
Quote from: nugax
  F: textfile;
  sFileName : string;
  TDirList : TStringList;
  iNumbdir : integer;
-Nugax

nugax

  • Full Member
  • ***
  • Posts: 232
Re: Find Directories in path and put in TStringList
« Reply #22 on: February 26, 2022, 09:04:16 pm »
In case anyone else is looking, this was the simple solution i was looking for:

from unit: fileutil;

fileutil.FindAllDirectories(DirList, '/home/cadams/git/cyberbbs/build/sessions/', False);


The FindAllFiles, wouldnt return empty dir, etc. This routine simply returns a list of directories in a given path.
-Nugax

 

TinyPortal © 2005-2018