Link to home
Start Free TrialLog in
Avatar of RollinNow
RollinNow

asked on

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?
Avatar of mrichmon
mrichmon

Are you looking for a specific email address throughout the file or any email addresses after that string?
Avatar of RollinNow

ASKER

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.

You will want to use regular expressions for this.  I am not good enough to give you the exact regular expression you would need....
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.
SOLUTION
Avatar of mrichmon
mrichmon

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
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?
Sounded to me like your plan was to read it in w/ cfhttp?
<CFHTTP Method="GET"
URL="http://somewhere/exported.cfm"
Redirect="No">
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)>
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?
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?
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.
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.


ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That works very well, and will save me hours of time. Thanks so much for your help!