Recent

Author Topic: Findallfiles question  (Read 1036 times)

Ten_Mile_Hike

  • Jr. Member
  • **
  • Posts: 91
Findallfiles question
« on: February 20, 2024, 09:10:11 pm »
I know that the following code doesn't use Try/Finally or create/free a Tstringlist.
It is just a simple example to get my question across.

The procedure below finds only 1345 *.txt files on my C: drive
but the excellent freeware program "Everything" finds 2232 *.txt files there.
Investigation finds that my code can not parse certain folders on my drive.
I assume that this is because of "Permissions". I have tried running the
executable outside of Lazarus "as admin", with no help.

HOW CAN I LIST ALL 2232 FILES ON MY C: DRIVE LIKE "EVERYTHING" DOES?

Latest Laz/FPC release, Win 11

procedure TForm1.Button1Click(Sender: TObject);
begin
  memo1.Lines  := FindAllFiles('.\C:\', '*.txt', True);
end; 


Thank You
When any government, or any church for that matter, undertakes to say to its subjects, This you may not read, this you
must not see, this you are forbidden to know, the end result is tyranny and oppression no matter how holy the motives.

Robert A. Heinlein

KodeZwerg

  • Hero Member
  • *****
  • Posts: 2269
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Findallfiles question
« Reply #1 on: February 20, 2024, 09:27:28 pm »
Be aware that tools like "Everything" also list archive or cache/temp folder data ...
Untested, just written in here so there might be errors.
Code: Pascal  [Select][+][-]
  1. function FindFiles(const Path, Mask: string): TStringList;
  2. var
  3.   SearchRec: TSearchRec;
  4.   CurrentPath: string;
  5. begin
  6.   Result := TStringList.Create;
  7.   try
  8.     if Path[Length(Path)] <> '\' then
  9.       CurrentPath := Path + '\'
  10.     else
  11.       CurrentPath := Path;
  12.  
  13.     if FindFirst(CurrentPath + Mask, faAnyFile, SearchRec) =  0 then
  14.     begin
  15.       repeat
  16.         Result.Add(CurrentPath + SearchRec.Name);
  17.       until FindNext(SearchRec) <>  0;
  18.       FindClose(SearchRec);
  19.     end;
  20.  
  21.     if FindFirst(CurrentPath + '*', faDirectory, SearchRec) =  0 then
  22.     begin
  23.       repeat
  24.         if (SearchRec.Attr and faDirectory <>  0) and (SearchRec.Name <> '.') and (SearchRec.Name <> '..') then
  25.           Result.AddStrings(FindFiles(CurrentPath + SearchRec.Name, Mask));
  26.       until FindNext(SearchRec) <>  0;
  27.       FindClose(SearchRec);
  28.     end;
  29.   except
  30.     Result.Free;
  31.     raise;
  32.   end;
  33. end;
I guess the TStringList handling must be outsourced, right now it destroy itself I think :D
« Last Edit: Tomorrow at 31:76:97 xm by KodeZwerg »

Ten_Mile_Hike

  • Jr. Member
  • **
  • Posts: 91
Re: Findallfiles question
« Reply #2 on: February 20, 2024, 09:50:07 pm »
KodeZwerg,

Thank You. I will play with this this tomorrow.
I am surprised and relieved that it was not a
Windows "Permissions"/"Ownership" thingy.
When any government, or any church for that matter, undertakes to say to its subjects, This you may not read, this you
must not see, this you are forbidden to know, the end result is tyranny and oppression no matter how holy the motives.

Robert A. Heinlein

TRon

  • Hero Member
  • *****
  • Posts: 3844
Re: Findallfiles question
« Reply #3 on: February 20, 2024, 09:58:30 pm »
@Ten_Mile_Hike:
I do not know the program you refer to but a quick glance at their webpage seem to suggest that the program is able to export a filelist (does require the program can export filenames with their full path).

Do export and then load the produced list into a stringlist. Then use the code from KodeZwerg to create a simlar list (full paths included) and load that into another stringlist. Now you can compare the two lists and try to determine which files are missing (if any). Make sure to allow for the stringlist to accept duplicates. (brain-farth, see also below. Thx for noticing Zvoni)

Instead of KodeZwerg's code you could also check against the findallfiles results from you own code.
« Last Edit: February 21, 2024, 10:56:50 am by TRon »
I do not have to remember anything anymore thanks to total-recall.

Bart

  • Hero Member
  • *****
  • Posts: 5497
    • Bart en Mariska's Webstek
Re: Findallfiles question
« Reply #4 on: February 20, 2024, 10:30:44 pm »
Well, I tried similar (for stress testing) and got an exception because FindAllFiles tried to access folders it wasn't supposed to.
So that's gonna be an issue.

Bart

Zvoni

  • Hero Member
  • *****
  • Posts: 2800
Re: Findallfiles question
« Reply #5 on: February 21, 2024, 08:54:31 am »
Make sure to allow for the stringlist to accept duplicates.

Whatever for????
A file including its complete Path is UNIQUE for the Volume it resides on.
If you take into account, that on Windows you have different Letters for "Drives", and on *nix you have a complete folder-tree starting at root (independent if there are mutiple physical Disks mounted somewhere in the tree), it's even UNIQUE across the board (=Computer)
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

TRon

  • Hero Member
  • *****
  • Posts: 3844
Re: Findallfiles question
« Reply #6 on: February 21, 2024, 10:55:39 am »
Whatever for????
Are you familiar with the situation that your mind is saying one thing but your hands are still in the process of writing something down that happened 10 thoughts ago ? And that sometimes these two things mingle ?

It is a poor excuses from my side and one which is (or can be) expensive to the reader, so my apologies because you are absolutely right Zvoni. The thing I wrote down and that you quoted makes no sense whatsoever  :-[

Should be corrected now. Thank you for having brought that to my attention.
« Last Edit: February 21, 2024, 10:59:46 am by TRon »
I do not have to remember anything anymore thanks to total-recall.

Zvoni

  • Hero Member
  • *****
  • Posts: 2800
Re: Findallfiles question
« Reply #7 on: February 21, 2024, 11:21:58 am »
Should be corrected now. Thank you for having brought that to my attention.
No Problem.
I suffer from the occasional brainfart, too

EDIT: The idea with the two StringLists is not a bad one, but I'd probably go for an InMemory (SQLite) Database with two tables.
Throw the first result into table1, the second result into table2, then Query with a Full outer join, which file (incl. Paths) is in which table resp. which file is missing where.
Pretty easy, IMO
« Last Edit: February 21, 2024, 11:26:16 am by Zvoni »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

 

TinyPortal © 2005-2018