WinBatch® Technical Support Forum

All Things WinBatch => WinBatch => Topic started by: pzig98 on December 12, 2013, 09:32:47 AM

Title: Find email address
Post by: pzig98 on December 12, 2013, 09:32:47 AM
Trying to write a script to delete email address from a database.

I need to pluck out the address from a text file as my script reads it line by line

Some example lines,

X-Original-To: John@Doe.com
[Sample@yahoo.com]: host mta7.am0.yahoodns.net[66.196.118.33] said: 554
[user@foxinv.com]: host inbound.foxinv.com.netsolmail.net[206.188.198.64]
delivery error: dd Sorry your message to test@yahoo.com cannot be


I am guess that all mail servers do not format their return messages the same my thinking was to find every address contained and delete them from our database.

I am finding that they are either wrapped in spaces or [ ] which can easily be removed..

Any help?

Thanks

Title: Re: Find email address
Post by: Deana on December 12, 2013, 10:18:06 AM
Here is my undebugged attempt:

Code (winbatch) Select
;X-Original-To: John@Doe.com
;[Sample@yahoo.com]: host mta7.am0.yahoodns.net[66.196.118.33] said: 554
;[mfain@foxinv.com]: host inbound.foxinv.com.netsolmail.net[206.188.198.64]
;delivery error: dd Sorry your message to test@yahoo.com cannot be

emaildb = 'c:\temp\emaildb.txt'


read_hdl = FileOpen( emaildb, "Read")
linenum = 1
While @TRUE ; Loop till break do us end
   line = FileRead(read_hdl)
   If line == "*EOF*" Then Break

   ;Trick to handle email address at beginning and end of line
   line = @lf:line:@cr
   
   ; Search for brackets
   emailfound = 0
   ptr = 1
   While emailfound == 0
     
     openbracket_ptr =  StrIndex( line, '[', ptr, @Fwdscan )
     If openbracket_ptr == 0 then break
     closebracket_ptr =  StrIndex( line, ']', openbracket_ptr, @Fwdscan )
     If openbracket_ptr == 0 then break
     ; Extract data in Brackets if contains @ move on
     bracketdata = StrSub( line, openbracket_ptr+1, closebracket_ptr - (openbracket_ptr+1) )
     ;Pause(0,bracketdata)
     at_ptr = StrIndex( bracketdata, '@', 1, @Fwdscan )
     If at_ptr != 0
        emailaddr = bracketdata
        emailfound = 1
        break
     endif
     ptr = closebracket_ptr+1
   Endwhile
   
   ; No brackets found, locate email using only at sign
   If emailfound == 0
      at_ptr = StrIndex( line, '@', 1, @Fwdscan )
      If at_ptr == 0 then break
     
      ; Look for leading space or @LF
      begin_ptr = StrIndex( line, ' ', at_ptr, @Backscan )
      If begin_ptr == 0
         begin_ptr = StrIndex( line, @lf, at_ptr, @Backscan )
         If begin_ptr == 0
             Pause('Notice','No email info found on line')
             Break
         Endif
      Endif

      ; Look from trailing space or @CR
      end_ptr = StrIndex( line, ' ', at_ptr, @Fwdscan )
      If end_ptr == 0
         end_ptr = StrIndex( line, @cr, at_ptr, @Fwdscan )
         If end_ptr == 0 
             Pause('Notice','No email info found on line')
             Break
         Endif
      Endif



      emailaddr = StrSub( line, begin_ptr, end_ptr - begin_ptr )
      emailfound = 1
   Endif


   If emailfound == 1
      Pause( linenum, emailaddr )
   Endif
   linenum = linenum + 1
EndWhile

FileClose(read_hdl)




Title: Re: Find email address
Post by: pzig98 on December 12, 2013, 10:29:03 AM
Worked great on a small test file but then fails on a real bounced email.  Attached is text of real file with the actual name changed to "findme"
Title: Re: Find email address
Post by: Deana on December 12, 2013, 02:13:41 PM
UNDEBUGGED Attempt number two. This should help get you started coding....

Code (winbatch) Select

emaildb = 'c:\temp\test.txt'

read_hdl = FileOpen( emaildb, "Read")
linenum = 1
While @TRUE ; Loop till break do us end
   line = FileRead(read_hdl)
   If line == "*EOF*" Then Break

   ;Trick to handle email address at beginning and end of line
   line = @lf:line:@cr

   ; Locate email using only at sign
   at_ptr = StrIndex( line, '@', 1, @Fwdscan )
   If at_ptr == 0
      linenum = linenum+1
      continue
   Endif
     
   ; Look for leading space or @LF
   begin_ptr = StrIndex( line, ' ', at_ptr, @Backscan )
   If begin_ptr == 0
      begin_ptr = StrIndex( line, @lf, at_ptr, @Backscan )
      If begin_ptr == 0
             Pause('Notice','No email info found on line')
             Continue
       Endif
   Endif

   ; Look from trailing space or @CR
   end_ptr = StrIndex( line, ' ', at_ptr, @Fwdscan )
   If end_ptr == 0
      end_ptr = StrIndex( line, @cr, at_ptr, @Fwdscan )
      If end_ptr == 0 
          Pause('Notice','No email info found on line')
          Continue
      Else
          emailfound = 1
          emailaddr = StrSub( line, begin_ptr, end_ptr - begin_ptr )
          ; Remove brackets
          emailaddr = StrReplace( emailaddr, "[", "" )
          emailaddr = StrReplace( emailaddr, "]", "" )
      Endif
   Endif

   If emailfound == 1
      Pause( linenum, emailaddr )
   Endif
   linenum = linenum + 1
EndWhile
FileClose(read_hdl)
Title: Re: Find email address
Post by: pzig98 on December 13, 2013, 12:02:57 PM
Yup.  Works great.  Tnx!