• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 260
  • Last Modified:

Identify string in file and export it

Is there a CF tag or a technique to search through a file, such as cfm, or even a text file, and find all occurances of an email address?

The string I'm look to identify goes like this:

E-mail address: myname@domain.com"

Note and save the email address which follows the colon and the space, to text file, eeach email address on a separate line.  Filename is exported.cfm and is in the same directory as the code.

I thought of using CFHTTP and that's why I saved my exported file, exported.cfm, as a coldfusion .cfm file. But I don't have a clue if CFHTTP can seach, find and isolate an email address after a specific string, and save it, I guess, using CFFILE.  But how, exactly?
0
RollinNow
Asked:
RollinNow
  • 7
  • 5
  • 3
2 Solutions
 
mrichmonCommented:
Are you looking for a specific email address throughout the file or any email addresses after that string?
0
 
RollinNowAuthor Commented:
Any email address. So, that would be any string which follows the colon and the space, and up to the quote after the email address.

E-mail address: myname@domain.com"

The string "E-mail address: " will always be found so it can be used to mark where the email address will be taken, and where it ends.

0
 
mrichmonCommented:
You will want to use regular expressions for this.  I am not good enough to give you the exact regular expression you would need....
0
Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

 
aseusaincCommented:
You'll want to use findnocase to find 'E-mail address: ' and then the space that follows the actual address.  Then subtract the difference in start and stop location, then use mid() to extract the address.  Something like:

<cfset #start# = #findnocase('e-mail address: ','#cfhttp.filecontent#',1)#+16>
<cfset #end# = #findnocase(' ','#cfhttp.filecontent#','#start#')#>
<cfset #count# = #end# - #start#>
<cfset #email# = #mid('#cfhttp.filecontent#','#start#','#count#')#>

You'll want to modify this quite a bit so that you can loop through the entire document, always starting where your last #end# was.  You'll also probably want to use a conditional loop until your start returns a 0 (when cffind doesn't find what it's looking for)

Also, you can dump the results to a text file with cffile on each itteration of the loop like so:

<cffile action="append" file="c:\email.txt" output="#email#" addnewline="yes">

Hope this gives you a good starting point to get going.
0
 
mrichmonCommented:
That is a good idea actually, but terrible syntax.

aseusainc,

You should try to clean up code for yourself - and also since you are recommending it to others.

THis is a much better version of that:

<cfset start = findnocase('E-mail address: ',cfhttp.filecontent,1)+16>
<cfset end = findnocase(' ',cfhttp.filecontent, start)>
<cfset count = end - start>
<cfset email = mid(cfhttp.filecontent, start, count)>

By removing all of the extra useless # and also removing the string quotes that then re-evaluate inside - you will greatly increase the performance of the code.
0
 
aseusaincCommented:
Old newbie habit that I never could shake.  Sometimes I catch myself, most times I do not.  Just one of those things in life where you develop bad habits from teaching yourself, rather than being taught.
0
 
RollinNowAuthor Commented:
aseusainc or mrichmon,

I'll give that a try, but I'm a little confused. Where, or how do I reference the file which contains the email addresses, that is, the actual file exported.cfm?
0
 
aseusaincCommented:
Sounded to me like your plan was to read it in w/ cfhttp?
<CFHTTP Method="GET"
URL="http://somewhere/exported.cfm"
Redirect="No">
0
 
RollinNowAuthor Commented:
aseusainc:

I get this error:

email = mid(cfhttp.filecontent, start, count)
Error near line 10, column 7.
Parameter 3 of function Mid which is now "-16" must be a non-negative integer
The error occurred while processing an element with a general identifier of (CFSET),

Do I have the code structured correctly?

<CFHTTP Method="GET"
URL="http://www.domain.com/exported.cfm"
Redirect="No">

<cfset start = findnocase('E-mail address: ',cfhttp.filecontent,1)+16>
<cfset end = findnocase(' ',cfhttp.filecontent, start)>
<cfset count = end - start>
<cfset email = mid(cfhttp.filecontent, start, count)>
0
 
RollinNowAuthor Commented:
aseusainc:

Please ignore the last message. The error was apparently caused by a cfabort to keep other people out of the file.

There is now no error but the returned result is a blank page. What do I have to do with this code to see the output result?
0
 
RollinNowAuthor Commented:
I found an example and added this code to display the results.

<cfoutput>
 #cfhttp.filecontent#
</cfoutput>

But that displays everything, the entire page. What am I doing wrong?
0
 
aseusaincCommented:
Can you post (or better yet, host) the results.cfm so I can see the format of what you're importing?

My guess is that the seached string for start or end doesn't exist.
0
 
RollinNowAuthor Commented:
I can give you an example, if that will help. The actual file is huge, but for a test, I used only a few lines from exported.cfm, which gave the same result:

Using this code (with my working domain, of course):

<!--- start of parse.cfm code --->
<CFHTTP Method="GET"
URL="http://www.domain.com/exported.cfm"
Redirect="No">

<cfset start = findnocase('E-mail address: ',cfhttp.filecontent,1)+16>
<cfset end = findnocase(' ',cfhttp.filecontent, start)>
<cfset count = end - start>
<cfset email = mid(cfhttp.filecontent, start, count)>

<cfoutput>
 #cfhttp.filecontent#
</cfoutput>
<!--- end of code --->

Here is the exact contents of exported.cfm:
-------------
Send email to User@domain.net
E-mail address: user@bellsouth.net"

Jack Brown
E-mail address: user@yahoo.com"

Has been sent.
gary green
E-mail address: user@yahoo.co.uk"
-------------

Once parse.cfm is run, the entire contents of exported.cfm is displayed, word-wrapped of course:

Send email to User@domain.net E-mail address: user@bellsouth.net" Jack Brown E-mail address: user@yahoo.com" Has been sent. gary green E-mail address: user@yahoo.co.uk"


Hope that helps point out what I'm doing wrong.


0
 
aseusaincCommented:
I went ahead and wrote the whole thing for you.  This has been tested and confirmed working.  It will create an emails.txt file on the root of C: on your CF server (make sure the CF account has write permissions)

<cfhttp method="get" url="http://192.168.15.122/results.cfm" redirect="no">
</cfhttp>

<cfoutput>
  <cfset laststart = 2>
  <cfloop condition="laststart NEQ 1">
    <cfset laststart = #findnocase('e-mail address: ',cfhttp.filecontent,laststart)#>
    <cfset start = laststart + 16>
    <cfset laststart = laststart + 1>
    <cfif laststart NEQ 1>
        <cfset end = #findnocase('"',cfhttp.filecontent,start)#>
      <cfset count = end - start>
      <cfset email = #mid(cfhttp.filecontent,start,count)#>
        <cffile action="append" file="c:\emails.txt" output="#email#" addnewline="yes">
      </cfif>
  </cfloop>
</cfoutput>
0
 
RollinNowAuthor Commented:
That works very well, and will save me hours of time. Thanks so much for your help!

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 7
  • 5
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now