Entwickler-Ecke
Dateizugriff - How to make a search function!?!?
Stianbl - Sa 10.07.04 04:27
Titel: How to make a search function!?!?
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
Peter Lustig: Topic aus Windows API verschoben am Sa 10.07.2004 um 11:28
mirage228 - Sa 10.07.04 07:56
Titel: Re: How to make a search function!?!?
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.
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) = 0) and (Assigned(List)) then begin if FindFirst(sPath + sExt, faAnyFile, f2) = 0 then begin List.add(sPath + f2.Name); while (FindNext(f2) = 0) do 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):
Delphi-Quelltext
1: 2: 3: 4:
| procedure TForm1.Button1Click(Sender: TObject); begin FileList('C:\', '*.txt', True, Memo1.Lines); end; |
greets
mirage228
Stianbl - Sa 10.07.04 08:46
Titel: Re: How to make a search function!?!?
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 - Sa 10.07.04 09:16
Titel: Re: How to make a search function!?!?
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:
Delphi-Quelltext
1: 2: 3: 4: 5: 6: 7:
| procedure TForm1.Button1Click(Sender: TObject); begin 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
Stianbl - Sa 10.07.04 09:46
Titel: Re: How to make a search function!?!?
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 - Sa 10.07.04 10:05
Titel: Re: How to make a search function!?!?
But how do I include Subfolders?
Delete - 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 - Sa 10.07.04 18:44
Titel: Re: How to make a search function!?!?
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:\)
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
Delete - 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 - So 11.07.04 01:37
Titel: Re: How to make a search function!?!?
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:>
Delete - So 11.07.04 08:48
Titel: Re: How to make a search function!?!?
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: |
Delphi-Quelltext 1: 2:
| FileList('C:\', '*.sav', 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 |
"NET SEND", huh? You've come to the right place, I guess. :mrgreen:
Stianbl - So 11.07.04 13:20
Titel: Re: How to make a search function!?!?
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:>
Delete - 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:
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 CurrentPath := GetCurrentDir;
while(CurrentPath[length(CurrentPath)] = '\') do SetLength(CurrentPath,length(CurrentPath) - 1);
res := FindFirst(CurrentPath + '\*.*',faAnyFile,ds); while(res = 0) do begin if(ds.Name <> '.') and (ds.Name <> '..') and (ds.Attr and faDirectory <> 0) then begin SetCurrentDir(ds.Name); Scan(RootPath,Extension,List); end;
res := FindNext(ds); end; FindClose(ds);
res := FindFirst(CurrentPath + '\' + Extension, faAnyFile,ds); while(res = 0) do begin if(ds.Attr and faDirectory = 0) then List.Add(Format('%s\%s',[CurrentPath,ds.Name]));
res := FindNext(ds); end; FindClose(ds);
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 - So 11.07.04 18:42
Thanks! I`ll try it now...
Entwickler-Ecke.de based on phpBB
Copyright 2002 - 2011 by Tino Teuber, Copyright 2011 - 2025 by Christian Stelzmann Alle Rechte vorbehalten.
Alle Beiträge stammen von dritten Personen und dürfen geltendes Recht nicht verletzen.
Entwickler-Ecke und die zugehörigen Webseiten distanzieren sich ausdrücklich von Fremdinhalten jeglicher Art!