Search email

How do you search for an email address in text files?? Please help. My email is creator@magix.com.sg
marinedestroyer2Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

h_mohsenianCommented:
if i understand perfectly, u need a code that find all  e-mails in a textfile and for example add them to a listbox.

for execute the folowing code u must change Filename as ur file name and use a TListbox. i used spaces over a email address ! ( i have not any better way ! )

procedure TForm1.Button1Click(Sender: TObject);
var
  infile : textfile;
  filename, s : string;
  p : integer;
  i,p1,p2 : integer;
begin
Filename := 'mytext.txt'
assignFile(infile,Filename);
reset(infile);
while not eof(infile) do
begin
  readln(infile,s);
  while pos('@',s) > 0 do
  begin
    p := pos('@',s);
    for i := p downto 1 do
      if s[i] = ' ' then break;
    p1 := i+1;
    for i := p to length(s) do
      if s[i] = ' ' then break;
    p2 := i-1;
    ListBox1.Items.Add(copy(s,p1,p2-p1+1));
    delete(s,p,1);
  end;
end;
closefile(infile);
end;


best regards
hamed
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
h_mohsenianCommented:
i had a mistake : space is not the just seperator char , now i use valid chars for email , the both for loops convert to :

   for i := p downto 1 do
     if not (s[i] in ['0'..'9','.','a'..'z','A'..'Z']) then break;
   p1 := i+1;
   for i := p to length(s) do
     if not (s[i] in ['0'..'9','.','a'..'z','A'..'Z']) then break;
   
maybe i have more mistake too ;-)

hamed
0
marinedestroyer2Author Commented:
Thank you......your script is great except that this part doesn't work....nonetheless....you deserve the points thank you.
:

  for i := p downto 1 do
    if not (s[i] in ['0'..'9','.','a'..'z','A'..'Z']) then break;
  p1 := i+1;
  for i := p to length(s) do
    if not (s[i] in ['0'..'9','.','a'..'z','A'..'Z']) then break;
0
PMI ACP® Project Management

Prepare for the PMI Agile Certified Practitioner (PMI-ACP)® exam, which formally recognizes your knowledge of agile principles and your skill with agile techniques.

marinedestroyer2Author Commented:
One more thing, I am willing to give more points if you can tell me how to do this:
you see, I am trying to make this filter system where it scans the whole drive for .txt files and then it searches words in the text file and list it out. I have already done the searching file program but I do not know how to add it to this searching words program. My searching file program is below

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    ListBox1: TListBox;
    Memo1: TMemo;
    Button1: TButton;
    procedure Button1Click(Sender:Tobject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

  function FindFile(const filespec: TFileName; attributes: integer= faReadOnly Or faHidden Or faSysFile Or faArchive): TStringList;

var
  Form1: TForm1;



  implementation

  function FindFile(const filespec: TFileName;
                    attributes: integer): TStringList;
  var
    spec: string;
    list: TStringList;

  procedure RFindFile(const folder: TFileName);
  var
    SearchRec: TSearchRec;
  begin
    // Locate all matching files in the current
    // folder and add their names to the list
    if FindFirst(folder + spec, attributes, SearchRec)=0 then begin
      try
        repeat
          if (SearchRec.Attr and faDirectory = 0) or
             (SearchRec.Name<>'.') and (SearchRec.Name<>'..') then
            list.Add(folder + SearchRec.Name);
        until FindNext(SearchRec) <> 0;
      except
        FindClose(SearchRec);
        raise;
      end;
      FindClose(SearchRec);
    end;
    // Now search the subfolders
    if FindFirst(folder + '*', attributes
        Or faDirectory, SearchRec) = 0 then
    begin
      try
        repeat
          if ((SearchRec.Attr and faDirectory) <> 0) and
             (SearchRec.Name<>'.') and (SearchRec.Name<>'..') then
            RFindFile(folder + SearchRec.Name + '\');
        until FindNext(SearchRec) <> 0;
      except
        FindClose(SearchRec);
        raise;
      end;
      FindClose(SearchRec);
    end;
  end; // procedure RFindFile inside of FindFile

  begin // function FindFile
    list := TStringList.Create;
    try
      spec := ExtractFileName(filespec);
      RFindFile(ExtractFilePath(filespec));
      Result := list;
    except
      list.Free;
      raise;
    end;
  end;




  procedure TForm1.Button1Click(Sender: TObject);
  var
    list: TStringList;
  begin
    list := FindFile('C:\*.pas');
    ListBox1.Items.Assign(list);
    list.Free;
  end;

{$R *.DFM}

end.

0
h_mohsenianCommented:
thanks marinedestroyer2,

my second code had just a little bug :

in the first loop p changes to p-1 and in second p changes to p+1 then we pass the '@' char , infact :


 for i := p-1 downto 1 do
   if not (s[i] in ['0'..'9','.','a'..'z','A'..'Z']) then break;
 p1 := i+1;
 for i := p+1 to length(s) do
   if not (s[i] in ['0'..'9','.','a'..'z','A'..'Z']) then break;

i suggested u this code becouse it is more complete.

sorry , now i have not delphi ( where i am now ). i will see your code later.

best regards
hamed
0
h_mohsenianCommented:
i think that is better u ask ur q as a dependent q , therefor u can get yr need faster.

sorry again
hamed
0
h_mohsenianCommented:
i see ur code again and now have Delphi,

i think u want a programm that search all of the .txt files in a directory ( and its subdirectories ) and find all emails that had written in that files and add them to a Listbox( not add filenames to a listbox )

now if it is exactly ur need. I worte ur app ( by a little correction of ur code and my first code ). give me your email. i will send u the application.

best regards
hamed
0
h_mohsenianCommented:
sorry, it seems your new q is deferent to ur last q.is your example in your q, is just an example or is ur exact need
0
h_mohsenianCommented:
the code is : ( see it , if it was you need us it )

all of them are important , ichanged some lines :

unit email2Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Memo1: TMemo;
    ListBox1: TListBox;
    ListBox2: TListBox;
    procedure Button1Click(Sender: TObject);
  private
    procedure FindEmailsFromFile(filename : string);    { Private declarations }
    function FindFile(const filespec: TFileName; attributes: integer= faReadOnly Or faHidden Or faSysFile
      Or faArchive): TStringList;
  public
    { Public declarations }
  end;



var
  Form1: TForm1;

implementation



procedure TForm1.FindEmailsFromFile(filename : string);
var
infile : textfile;
s : string;
p : integer;
i,p1,p2 : integer;
begin
assignFile(infile,Filename);
reset(infile);
while not eof(infile) do
begin
 readln(infile,s);
 while pos('@',s) > 0 do
 begin
   p := pos('@',s);
   for i := p-1 downto 1 do
     if not (s[i] in ['0'..'9','.','a'..'z','A'..'Z']) then break;
   p1 := i+1;
   for i := p+1 to length(s) do
     if not (s[i] in ['0'..'9','.','a'..'z','A'..'Z']) then break;
   p2 := i-1;
   ListBox2.Items.Add(copy(s,p1,p2-p1+1));
   delete(s,p,1);
 end;
end;
closefile(infile);
end;



 function TForm1.FindFile(const filespec: TFileName;
                   attributes: integer): TStringList;
 var
   spec: string;
   list: TStringList;
 procedure RFindFile(const folder: TFileName);
 var
   SearchRec: TSearchRec;
 begin
   // Locate all matching files in the current
   // folder and add their names to the list
   if FindFirst(folder + spec, attributes, SearchRec)=0 then begin
     try
       repeat
         if (SearchRec.Attr and faDirectory = 0) or
            (SearchRec.Name<>'.') and (SearchRec.Name<>'..') then
            begin
              list.Add(folder + SearchRec.Name);
              FindEmailsFromFile(folder + SearchRec.Name);
            end;
       until FindNext(SearchRec) <> 0;
     except
       FindClose(SearchRec);
       raise;
     end;
     FindClose(SearchRec);
   end;
   // Now search the subfolders
   if FindFirst(folder + '*', attributes
       Or faDirectory, SearchRec) = 0 then
   begin
     try
       repeat
         if ((SearchRec.Attr and faDirectory) <> 0) and
            (SearchRec.Name<>'.') and (SearchRec.Name<>'..') then
           RFindFile(folder + SearchRec.Name + '\');
       until FindNext(SearchRec) <> 0;
     except
       FindClose(SearchRec);
       raise;
     end;
     FindClose(SearchRec);
   end;
 end; // procedure RFindFile inside of FindFile

 begin // function FindFile
   list := TStringList.Create;
   try
     spec := ExtractFileName(filespec);
     RFindFile(ExtractFilePath(filespec));
     Result := list;
   except
     list.Free;
     raise;
   end;
 end;

procedure TForm1.Button1Click(Sender: TObject);
 var
   list: TStringList;
 begin
   list := FindFile('E:\hamed\*.txt');
   ListBox1.Items.Assign(list);
   list.Free;
 end;

 {$R *.DFM}

end.



0
h_mohsenianCommented:
marinedestroyer2, are you here?

could that code help you?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.