Recent

Author Topic: read all files in a folder  (Read 12934 times)

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
read all files in a folder
« on: July 19, 2010, 09:20:17 am »
Question: What are the commands to read all files in a folder and the subfolders too? I might do crossplatform an example? Thanks
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: read all files in a folder
« Reply #1 on: July 19, 2010, 09:46:43 am »
By using TFileSearcher:
Code: [Select]
uses
  FileUtil;
...

type
  TFileReader = class(TFileSearcher)
  public
    procedure OnFileFound(FileIterator: TFileIterator);
  end;

...
procedure TFileReader.OnFileFound(FileIterator: TFileIterator);
begin
  // Read the file here
end;
...
var
  Searcher: TFileReader;
begin
  Searcher := TFileReader.Create;
  Searcher.OnFileFound := @Searcher.OnFileFound;
  // search the files on C:\Files directory
  // search only on .txt and .html files
  // recursive
  // use ; as mask separator (to separate *.txt and *.html)
  Searcher.Search('C:\Files','*.txt;*.html',true,';');
end;
...

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
Re: read all files in a folder
« Reply #2 on: July 19, 2010, 10:22:34 am »
Thank you. But get an error!


Hint: Start of reading config file C:\lazarus\fpc\2.2.4\bin\i386-win32\fpc.cfg
Hint: End of reading config file C:\lazarus\fpc\2.2.4\bin\i386-win32\fpc.cfg
Free Pascal Compiler version 2.2.4 [2009/10/25] for i386
Copyright (c) 1993-2008 by Florian Klaempfl
Target OS: Win32 for i386
Compiling project1.lpr
Compiling unit1.pas
Compiling unit1.pas
unit1.pas(35,26) Error: Wrong number of parameters specified for call to "OnFileFound"
ricercafile.pas(54,23) Hint: Found declaration: TFileReader.OnFileFound(TFileIterator);
unit1.pas(48) Fatal: There were 1 errors compiling module, stopping
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: read all files in a folder
« Reply #3 on: July 19, 2010, 02:04:34 pm »
Sorry, it's an untested code. What mode are you in? This should work in the default objfpc mode.
What does line 35 contain?

Let's see...try changing the code:
Code: [Select]
type
  TFileReader = class(TFileSearcher)
  public
    constructor Create;
    procedure OnFileFound(FileIterator: TFileIterator);
  end;
...
constructor TFileReader.Create;
begin
  inherited Create;
  FOnFileFound := @OnFileFound;
end;
...
And remove this line:
Code: [Select]
Searcher.OnFileFound := @Searcher.OnFileFound;

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
Re: read all files in a folder
« Reply #4 on: July 19, 2010, 02:24:43 pm »
ricercafile.pas(62,17) Error: Identifier not found "FOnFileFound"
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
Re: read all files in a folder
« Reply #5 on: July 19, 2010, 03:43:51 pm »
Resolved!


unit RicercaFile;

{$mode objfpc}{$H+}


{ TForm1 }


{

type
  TFileReader = class(TFileSearcher)
  public
    constructor Create;
    procedure OnFileFound(FileIterator: TFileIterator);
  end;
...
constructor TFileReader.Create;
begin
  inherited Create;
  FOnFileFound := @OnFileFound;
end;
...
var
  Searcher: TFileReader;
begin
  Searcher := TFileReader.Create;
  Searcher.OnFileFound := @Searcher.OnFileFound;
  // search the files on C:\Files directory
  // search only on .txt and .html files
  // recursive
  // use ; as mask separator (to separate *.txt and *.html)
  Searcher.Search('C:\Files','*.txt;*.html',true,';');
end;
}


interface

uses
  Classes, SysUtils,
  FileUtil //aggiunto per leggere i file
  ;

type
  TFileReader = class(TFileSearcher)
  public
    constructor Create;
    procedure OnFileFoundNew(FileIterator: TFileIterator);
  end;

implementation
uses unit1;


constructor TFileReader.Create;
begin
  inherited Create;
   OnFileFound := @OnFileFoundNew;
end;

procedure TFileReader.OnFileFoundNew(FileIterator: TFileIterator);
begin
   Form1.Lista_File.Items.Add(FileIterator.Path + '-->' + FileIterator.FileInfo.Name + '(' + IntToStr(FileIterator.FileInfo.Attr) + ')');
end;

end.


//--------------------------------------------------------------------------------

unit Unit1;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
  StdCtrls,RicercaFile;

type

  { TForm1 }

  TForm1 = class(TForm)
    Button1: TButton;
    Lista_File: TListBox;
    procedure Button1Click(Sender: TObject);
  private
    { private declarations }
  public
    { public declarations }
  end;

var
  Form1: TForm1;

implementation

procedure TForm1.Button1Click(Sender: TObject);
var
   Searcher: TFileReader;
begin
     Searcher := TFileReader.Create;
     // search the files on C:\Files directory
     // search only on .txt and .html files
     // recursive
     // use ; as mask separator (to separate *.txt and *.html)
      Searcher.Search('C:\Lavoro','*.mdb;*.html',true,';');
end;

initialization
  {$I unit1.lrs}

end.
       

         
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
Re: read all files in a folder
« Reply #6 on: July 20, 2010, 10:33:59 am »
How do I know which drives are available and what not?

If instead of commands TFileReader had the lowest level where I can see the drive connected, view files and folders would be better. Anybody know these commands?
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

Leledumbo

  • Hero Member
  • *****
  • Posts: 8757
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: read all files in a folder
« Reply #7 on: July 20, 2010, 12:12:13 pm »
Quote
ricercafile.pas(62,17) Error: Identifier not found "FOnFileFound"
It was a guess actually since usually instance variables are prefixed with F.
Quote
How do I know which drives are available and what not?
I'm not sure, I don't think we have that in cross platform manner.

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
Re: read all files in a folder
« Reply #8 on: July 20, 2010, 12:50:12 pm »
Thank you
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

xinyiman

  • Hero Member
  • *****
  • Posts: 2256
    • Lazarus and Free Pascal italian community
Re: read all files in a folder
« Reply #9 on: July 22, 2010, 01:35:16 pm »
RESOLVED, seample example

//to write file and directory into ListBox

procedure TForm1.Button1Click(Sender: TObject);
var
//   Searcher: TFileReader;
  Info : TSearchRec;
  Count : Longint;
  app: string;
begin
     Count:=0;
     //se volessi leggere la cartella corrente basterebbe *
     If FindFirst ('c:\*',faAnyFile and faDirectory,Info)=0 then
       begin
       Repeat
         Inc(Count);
         With Info do
           begin
           If (Attr and faDirectory) = faDirectory then
             Form1.Lista_File.Items.Add('Dir : ' + Name + ' dimensione: ' + IntToStr(Size))
           else
            Form1.Lista_File.Items.Add('File : ' + Name + ' dimensione: ' + IntToStr(Size));
         end;
       Until FindNext(info)<>0;
       end;
     FindClose(Info);
     //Writeln ('Finished search. Found ',Count,' matches');
     Form1.Lista_File.Items.Add('Finished search. Found ' + IntToStr(Count) + ' matches');
end;


//to control the drive

procedure TForm1.Button2Click(Sender: TObject);
begin
     if DirectoryExists('C:')=TRUE then
     begin
          ShowMessage('Il drive C: esiste!');
     end;

     if DirectoryExists('E:')=TRUE then
     begin
          ShowMessage('Il drive E: esiste!');
     end;
end;     
Win10, Ubuntu and Mac
Lazarus: 2.1.0
FPC: 3.3.1

 

TinyPortal © 2005-2018