PDA

View Full Version : Searching directories for files



DarknessX
02-05-2007, 02:04 AM
I saw this on the web somewhere but for the life of me can't find it now...
How do I find specific files from a directory? IE,



Directory -->
yu.txt
loki.uj
blah.uj
ju.uj


I want to find all files, no matter their names, with a .uj extension (not really, but thats an example) and take the name of the file (without the extension, IE, varone := loki) and assign it to a variable.

How would I do this?

wodzu
02-05-2007, 07:13 AM
Hello.

I think that those functions are what you are looking for: FindFirst, FindNext, FindClose.

Good luck.

DarknessX
02-05-2007, 10:49 AM
Do you think you could show me some examples of how to use those?
I looked on the FPC website and found nothing of them.I am in a hurry, so i will check again later...

technomage
02-05-2007, 11:10 AM
Jist Google for Free Pascal FindFirst and you will get

http://community.freepascal.org:10000/docs-html/rtl/sysutils/findfirst.html

Huehnerschaender
02-05-2007, 11:10 AM
To search for specific files on your harddisk, use findfirst and findnext as wodzu said.
This is easy if you just want to search in one directory.

A call of FindFirst searches for the first file and FindNext within a repeat-clause will search for every follwing files.

The following code capsulates the functionality to search recursively in all subdirectories too!

The procedure needs 5 parameters:

- Directory (e.g. 'C:\Programs')
- Searchmask (e.g. '*.*' or '*.doc', in your case *.uj)
- a list where to store the filenames (e.g. Listbox1.Items)
- recursive search (True searches all subdirs recursively, false searches only in the parameter-directory).
- ClearList lets the List be cleared before the new filenames are attached.


procedure GetFilesInDirectory(Directory: String; const Mask: String;
List: TStrings;
WithSubDirs, ClearList: Boolean);

procedure ScanDir(const Directory: String);
var
SR: TSearchRec;
begin
if FindFirst(Directory + Mask, faAnyFile and not faDirectory, SR) = 0 then try
repeat
List.Add(Directory + SR.Name)
until FindNext&#40;SR&#41; <> 0;
finally
FindClose&#40;SR&#41;;
end;

if WithSubDirs then begin
if FindFirst&#40;Directory + '*.*', faAnyFile, SR&#41; = 0 then try
repeat
if &#40;&#40;SR.attr and faDirectory&#41; = faDirectory&#41; and
&#40;SR.Name <> '.'&#41; and &#40;SR.Name <> '..'&#41; then
ScanDir&#40;Directory + SR.Name + '\'&#41;;
until FindNext&#40;SR&#41; <> 0;
finally
FindClose&#40;SR&#41;;
end;
end;
end;

begin
List.BeginUpdate;
try
if ClearList then
List.Clear;
if Directory = '' then Exit;
if Directory&#91;Length&#40;Directory&#41;&#93; <> '\' then
Directory &#58;= Directory + '\';
ScanDir&#40;Directory&#41;;
finally
List.EndUpdate;
end;
end;


Here is an example how to call the procedure:



procedure TForm1.Button1Click&#40;Sender&#58; TObject&#41;;
begin
GetFilesInDirectory&#40;'C&#58;\', '*.*', Listbox1.Items, False, True&#41;;
end;

In this example all filenames (*.*) in the directory 'C:\' are stored in 'Listbox1.Items'. Subdirectories are not searched (False).

You can now easily add your own functionalities at the position where the files are added to the list:


List.Add&#40;Directory + SR.Name&#41;

I hope this helps.

Note that the code and parts of the explanation are copied from a forum. I just translated it for your understanding.

Greetings,
Dirk

DarknessX
02-05-2007, 11:20 PM
Ok, well, I understand the majority, but the one thing... I'm taking a guess that that is OOP pascal. Is it possible to do it WITHOUT OOP? Because I don't know OOP, and am overly confused by OOP.

Also, is it possible for me to just copy&paste those exact procedures, modify the directory and filenames, and then call it like shown? I will do a few tests, see if I can get it working. The first method, FindFirst, FindNext, and FindClose should work good enough for me (I have a variable setup for the path to the program, and only the folder path/timers/ needs to be searched).

Edit: now that I look at it, I can also see it being delphi (not sure though, since I don't know delphi either, as it is an offshoot of OOP).

Edit2: Don't think it's workable in FPC. Thanks anyways :P

poitpoit
03-05-2007, 07:13 AM
hi darknessX. hope it works
Program Example43;

{ This program demonstrates the FindFirst function }

Uses SysUtils;

Var Info : TSearchRec;
Count : Longint;
fn : Array[1..100] of String; { filenames.. }

Begin
Count:=0;
If FindFirst ('*.fu',faAnyFile and faDirectory,Info)=0 then
begin
Repeat
Inc(Count);
With Info do
begin
fn[Count] := name;
{ Writeln (Name:40,Size:15); }
end;
Until (FindNext(info)<>0) OR (count=100);
end;
FindClose(Info);
Writeln ('Finished search. Found ',Count,' matches');
End.