Geo
Geo

Reputation: 3200

Search Folders in ColdFusion 9

I am trying to create a search method to attach files on my outbound emails. I need to search inside a folder and find all the files that begin with a certain character and then attach them to the email. I just need a head start on how you could create such a search method so any pointers or links to references will be appreciated.

This is What I have so far but it does not seem to work correct when I use a path instead of GetBaseTemplatePath()

<cfscript>
  attributes.attachments = 2011093475839213;
</cfscript>

<cfset Directory = "E:\sites\Example.com\FileFolder\#attributes.attachments#"> 


<cfset CurrentDirectory=Directory>  
<cfset CurrentDirectory=ListDeleteAt(CurrentDirectory,ListLen(CurrentDirectory,"/\"),"/\")>  

<cfoutput>  
 <b>Current Directory:</b> #CurrentDirectory#  
    <br />  
</cfoutput>  

<cfdirectory action="list" directory="#CurrentDirectory#" name="result">  
<cfdump var="#result#"> 

When I change the code slightly by

<cfset CurrentDirectory=GetBaseTemplatePath()> 

My code works and I get the list of all the files in my current directory. Do I have a mistake on my path that I cannot see?

This is my CFMAIL part which I do have an issue with. When I dump my #result# query I get all the files inside the folder. Then I get this error:

The resource 2011093475839213.pdf was not found.

The root cause was: ''.

I do receive an email despite the error, just not the attached files.

<!--- Email Test --->
<CFMAIL FROM="[email protected]" TO="[email protected]"  SUBJECT="Test" type="HTML">
<P> This is the attachment test</P>
<p> For this test to be successful, we need to receive some file attachments with this email</p>

    <cfloop query="result">

        <cfmailparam file="#result.name#" disposition="attachment">

    </cfloop>


</cfmail>
<!--- Email Test Ends --->

Upvotes: 0

Views: 656

Answers (2)

Jason M
Jason M

Reputation: 510

Something like this:

<cfdirectory action="list" directory="#myDirectory#" name="myDir">

<cfmail subject="My Subject" to="yourAddress" from="myAddress">
  My Message
  <cfsilent>
    <cfloop query="myDir">
      <cfif Left(myDir.name,1) eq "Z">
        <cfmailparam file="#myDirectory & myDir.name#">
      </cfif>
    </cfloop>
  </cfsilent>
</cfmail>

Upvotes: 1

Busches
Busches

Reputation: 1964

The cfdirectory tag will allow you to search through folder(s) with a specific pattern. Using the query it returns, you can loop over it and attach all the files you need to the email.

http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-7f99.html

Upvotes: 2

Related Questions