Results 1 to 7 of 7

Thread: Searching directories for files

  1. #1

    Searching directories for files

    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,

    Code:
    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?
    --MagicRPG--

  2. #2

    Searching directories for files

    Hello.

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

    Good luck.

  3. #3

    Searching directories for files

    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...
    --MagicRPG--

  4. #4

    Searching directories for files

    Jist Google for Free Pascal FindFirst and you will get

    http://community.freepascal.org:1000...findfirst.html
    <A HREF="http://www.myhpf.co.uk/banner.asp?friend=139328">
    <br /><IMG SRC="http://www.myhpf.co.uk/banners/60x468.gif" BORDER="0">
    <br /></A>

  5. #5

    Searching directories for files

    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.

    Code:
    procedure GetFilesInDirectory&#40;Directory&#58; String; const Mask&#58; String;
                                  List&#58; TStrings;
                                  WithSubDirs, ClearList&#58; Boolean&#41;;
    
    procedure ScanDir&#40;const Directory&#58; String&#41;;
    var
      SR&#58; TSearchRec;
    begin
      if FindFirst&#40;Directory + Mask, faAnyFile and not faDirectory, SR&#41; = 0 then try
        repeat
          List.Add&#40;Directory + SR.Name&#41;
        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:

    Code:
    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:

    Code:
    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
    <a href="http://www.greatgamesexperiment.com/game/Valgard/?utm_source=gge&amp;utm_medium=badge_game"><img border="0" alt="GGE" title="GGE" src="http://static.greatgamesexperiment.com/badge/game/valgard/gge400x56.png"></a>

  6. #6

    Searching directories for files

    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
    --MagicRPG--

  7. #7

    Searching directories for files

    hi darknessX. hope it works
    [pascal]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.
    [/pascal]

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •