Autor Beitrag
Stianbl
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 29



BeitragVerfasst: Sa 10.07.04 04:27 
Ok, first of all: Please reply in English!

I need to know how to make a search function in Delphi 6 that searches in the folder you set it to, and all the subfolders...

Does anybody know?
I really need it! :o)

Thanks in advance!


Moderiert von user profile iconPeter Lustig: Topic aus Windows API verschoben am Sa 10.07.2004 um 11:28
mirage228
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 338

Win 7 Prof.
Delphi 2005 Prof., Delphi 2010 Prof.
BeitragVerfasst: Sa 10.07.04 07:56 
Stianbl hat folgendes geschrieben:
Ok, first of all: Please reply in English!

I need to know how to make a search function in Delphi 6 that searches in the folder you set it to, and all the subfolders...

Does anybody know?
I really need it! :o)

Thanks in advance!


Hi,

try this:

Edit: Fixed error in the procedure.
ausblenden Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
procedure FileList(const sPath, sExt: String; bRecurse: Boolean; List: TStrings); 
var 
  f, f2: TSearchRec; 
begin 
  if (FindFirst(sPath + '*.*', faAnyFile, f) = 0and (Assigned(List)) then 
  begin 
    if FindFirst(sPath + sExt, faAnyFile, f2) = 0 then
    begin 
       List.add(sPath + f2.Name); 
       while (FindNext(f2) = 0do 
         List.Add(sPath + f2.Name); 
       FindClose(f2);
    end

    while FindNext(f) = 0 do
     if ((F.Attr and faDirectory) = faDirectory) and (f.name <>'.'and (f.name <> '..'and 
       (bRecurse) then 
         FileList(sPath + f.Name+'\', sExt, bRecurse, List);
  end
  FindClose(f); 
end;


Usage (finds all TXT-Files in C:\ and SubFolders and puts the result into a Memo):
ausblenden Delphi-Quelltext
1:
2:
3:
4:
procedure TForm1.Button1Click(Sender: TObject);
begin
  FileList('C:\''*.txt', True, Memo1.Lines);
end;


greets
mirage228

_________________
May the source be with you, stranger.


Zuletzt bearbeitet von mirage228 am Sa 10.07.04 09:17, insgesamt 1-mal bearbeitet
MathiasSimmack
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Sa 10.07.04 08:23 
What are you doing, mirage?
mirage228 hat folgendes geschrieben:
ausblenden Delphi-Quelltext
1:
2:
3:
    if FindFirst(sPath + '*.*', faAnyFile, f) = 0 then
    begin
     if FindFirst(sPath + sExt, faAnyFile, f2) = 0 then

And where is "FindClose" for "f2"? :?


btw: taken from the FAQs -

www.delphi-forum.de/...t=findfirst+findnext
www.delphi-forum.de/...t=findfirst+findnext
www.delphi-forum.de/...t=findfirst+findnext
www.delphi-forum.de/...t=findfirst+findnext
www.delphi-forum.de/...t=findfirst+findnext
Stianbl Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 29



BeitragVerfasst: Sa 10.07.04 08:46 
Could you make a little test to me?
I`m using Delphi 6!

If you could make a program that finds a file (in subdirs too) I would be very happy! :P

Can you? please please!!!! 8) Ok, I`ll stop!
mirage228
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 338

Win 7 Prof.
Delphi 2005 Prof., Delphi 2010 Prof.
BeitragVerfasst: Sa 10.07.04 09:16 
MathiasSimmack hat folgendes geschrieben:

What are you doing mirage?


:oops: You're right. I forgot the FindClose(F2);

Stianbl hat folgendes geschrieben:
Could you make a little test to me?
I`m using Delphi 6!

If you could make a program that finds a file (in subdirs too) I would be very happy! :P

Can you? please please!!!! 8) Ok, I`ll stop!


Hi,

Making such a program is very easy. You can do it yourself ;)


  • Open Delphi 6 [create a new application, if Delphi hasn't already created one]
  • Put 2 TEdit's (from "Standard") on your form. And a button.
    The first TEdit (Edit1) will contain the path with the directory in which the file,
    specified in the second TEdit (Edit2), will be searched.
    The search will be started by clicking the button (Button1).
    Also add a TMemo (Memo1) that will contain the search results.
  • Switch to the Source and Copy the (new) FileList procedure into the Unit (paste it after the {*.dfm} ).
  • Now switch back to the form and double-click the button. Delphi will generate an OnClick-Routine.
  • Write the following code into the generated procedure:
    ausblenden Delphi-Quelltext
    1:
    2:
    3:
    4:
    5:
    6:
    7:
    procedure TForm1.Button1Click(Sender: TObject);
    begin
      // insert this code:
      Memo1.Clear;
      FileList(IncludeTrailingPathDelimiter(Edit1.Text), Edit2.Text, True, Memo1.Lines); 
      // -----------------
    end;



Vóila, finished ;)

You can add some comfort-function (such as "Select File" and "Select Directory") if you want.

mfG
mirage228

_________________
May the source be with you, stranger.
Stianbl Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 29



BeitragVerfasst: Sa 10.07.04 09:46 
Thanks!
One final question: 8)
-------------------
What I would like to do, is to start the program, and push a button! Then the program deletes a file I have set it to in a game directory.. (a save game for example)
But the thing is, that the program have to find the file first! Since I will use this on other computers, and it should do it automaticly!
You understand?
-------------------
But thanks for the help! Thank you very much! :P

<edit: hmm.. something`s wrong with the FileList thing "[Error] Unit1.pas(32): Undeclared identifier: 'FileList'">
<edit: I made all the pieces, but I inserted the small source code... should I use the big? (the first one)>

<edit2: sorry! WOrks now! :P I put the big one first, then the small code! Thanks! But how could I do what I wrote some lines up?>

Thanks in advance!
Stianbl Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 29



BeitragVerfasst: Sa 10.07.04 10:05 
But how do I include Subfolders?
MathiasSimmack
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Sa 10.07.04 12:13 
@mirage: No, all I wanted to say was: You don't need the two TSearchRec variables. Just make one step (looking for folders), then the other (looking for files). That's the way, all FAQ samples work.
mirage228
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 338

Win 7 Prof.
Delphi 2005 Prof., Delphi 2010 Prof.
BeitragVerfasst: Sa 10.07.04 18:44 
Stianbl hat folgendes geschrieben:
But how do I include Subfolders?


Set the "bRecurse" parameter of the FileList procedure to "true". Then the procedure will also search in all subfolders of "sPath".

<Edit>
Stianbl hat folgendes geschrieben:
But how could I do what I wrote some lines up?

If you know what file you are searching for, you can directly set the parameter to that file.

Example (if you want to search for all *.sav files on your harddisk C:\, including all subfolders of C:\)
ausblenden Delphi-Quelltext
1:
  FileList('C:\''*.sav', True, Memo1.Lines);					

</Edit>

MathiasSimmack hat folgendes geschrieben:

@mirage: No, all I wanted to say was: You don't need the two TSearchRec variables. Just make one step (looking for folders), then the other (looking for files). That's the way, all FAQ samples work.


Yes, okay, you're right. I got this procedure somewhere in the Delphi-Praxis (that was about a year ago). It seemed to work, so I didn't care of the procedure itself, just used it...:oops:

greets
mirage228

_________________
May the source be with you, stranger.
MathiasSimmack
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Sa 10.07.04 19:18 
@mirage: See, I always try to improve those things a little bit. ;)

@Stianbl: Is this a regular Windows game with a setup and all of this? Then try to find the game's Registry entry (if available). Maybe you find the game's path there. So your scan should be faster because you don't need to scratch the whole hard drive. :mrgreen:
Stianbl Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 29



BeitragVerfasst: So 11.07.04 01:37 
Thank you very much guys! This really helps me to learn delphi better! :D
Really, really great answes I get here!
Thanks again for al the help!
I`m going to use this forum a bit :wink: Course this is great!!!

But is it possible to first search for a folder, and then find the file? Like if I want to make a program that searches for the folder "games", and the finds all the ".sav" files in this folder (and subfolders)? In this way, the searc would be faster if I didn`t find the path in the regestry! :)

<edit:> and where should I insert the "True" command? here:? "procedure FileList(const sPath, sExt: String; bRecurse: Boolean; List: TStrings);" And where? Could you insert it here for me so I see? I`m still just a big noob :oops:...
I`v made some small programs like a "paralellport controller that controles the lights in my computer, and a "net send" program I use to send messages with! :D </edit:>
MathiasSimmack
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: So 11.07.04 08:48 
Stianbl hat folgendes geschrieben:
<edit:> and where should I insert the "True" command? here:? "procedure FileList(const sPath, sExt: String; bRecurse: Boolean; List: TStrings);" And where? Could you insert it here for me so I see?

mirage did. Take a look at his sample call:
Zitat:
ausblenden Delphi-Quelltext
1:
2:
FileList('C:\''*.sav', True { <-- here is your big fat TRUE },
  Memo1.Lines);


Stianbl hat folgendes geschrieben:
I`m still just a big noob :oops:...

Yes, you are. ;)

Zitat:
I`v made some small programs like [...] a "net send" program I use to send messages with! :D

Suche in: Delphi-Forum, Delphi-Library "NET SEND", huh? You've come to the right place, I guess. :mrgreen:
Stianbl Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 29



BeitragVerfasst: So 11.07.04 13:20 
Hehe... But I don`t know German! :shock:
I`m from norway!
And the search in subfolders thing doesn`t work here... just in the main folder.. :(
I don`t know what`s wrong..

<edtd:> Well, I`t searches in subfolders when the filename is *.*, but Whan I search for game.dll that I have put in a folder called 123 on the c disc, I get no result.. :? What`s wrong? </edit:>
MathiasSimmack
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: So 11.07.04 17:18 
Zitat:
Hehe... But I don`t know German! :shock:
I`m from norway!

Hey, nobody is perfect. ;)
It was just a joke because you are not the only one with a "net send" utility.


Edit --

About your problem: I don't know what's wrong. However, this is the way, I hit the road:
ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
procedure scan(const RootPath: string; Extension: string; List: TStrings);
var
  CurrentPath : string;
  ds          : TSearchRec;
  res         : integer;
begin
  // what's the current folder?
  CurrentPath := GetCurrentDir;

  // remove a finishing backslash, if there is any
  while(CurrentPath[length(CurrentPath)] = '\'do
    SetLength(CurrentPath,length(CurrentPath) - 1);
  // Delphi 5 has no "RemoveTrailingBackslash"
  // function or something else like it; or maybe
  // I just don't know ... hm ...


  // get all folders
  res         := FindFirst(CurrentPath + '\*.*',faAnyFile,ds);
  while(res = 0do
  begin
    // name MUST NOT be "." and "..", ...
    if(ds.Name <> '.'and
      (ds.Name <> '..'and
    // and it has to be a directory
      (ds.Attr and faDirectory <> 0then
    begin
    // then jump into, & scan again
      SetCurrentDir(ds.Name);
      Scan(RootPath,Extension,List);
    end;

    // is there another folder?
    res := FindNext(ds);
  end;
  FindClose(ds);

  // now, get the file(s), I'm interested in
  res         := FindFirst(CurrentPath + '\' + Extension,
    faAnyFile,ds);
  while(res = 0do
  begin
    // it's not a folder, ...
    if(ds.Attr and faDirectory = 0then
    // ... add it to my list
      List.Add(Format('%s\%s',[CurrentPath,ds.Name]));

    // go find the next file
    res := FindNext(ds);
  end;
  FindClose(ds);

  // 1 level (= folder) up
  if(CurrentPath <> RootPath) then SetCurrentDir('..');
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  SetCurrentDir('c:\');
  Scan('c:\','g*.dll',ListBox1.Items);
end;

It works fine, but it is a bit different to mirage's function. But it shows me all libraries starting with a G -- no matter where they are.


All the badly English is Absicht! :mrgreen:
Stianbl Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 29



BeitragVerfasst: So 11.07.04 18:42 
Thanks! I`ll try it now...