structnew example

The following example shows usage of StructNew.

<cfset employee = StructNew()>

<cfset employee.first = "FirstName">
<cfset employee.last = "LastName">
<cfset employee.value1 = "12345">

<!--- Nested Structure... Have a structure inside a structure. --->
<cfset addr = structnew()>
<cfset addr.nu = "10">
<cfset addr.street = "BG Road">
<cfset addr.city = "Bangalore">

<cfset employee.address = "#addr#">

<cfdump var = #employee#>

You can download the source using the download button.

cfexchange - get messages example

This is an Example showing the usage of cfexchange tags.This example shows how to connect and get messages from the server. Note: You need to update the Application.cfm file with the appropriate values for exchange server, user ids, password, email ids.

<cftry>
<!--- Send an email, to verify --->
<cfmail to = "#email1#"   
      from = "#email2#"   
      subject = "Testing cfexchange message 1"   
      username = "#user1#"   
      password = "#password#"   
      server = "#exchangeServerIP#"   
      port = "25">

This message is sent by cfexchange test.
</cfmail>

<!--- Sleep for sometime so that the message is transferred/sent to the server. ---->
<cfscript>sleep(15000);</cfscript>

<!--- Connect to the server using cfexchangeconnection. ---->
<cfexchangeConnection
action="open"
username ="#user1#"
password="#password#"
server="#exchangeServerIP#"
connection="testconn1">


<!--- Get the mails from the server using cfexchangemail. ---->
<cfexchangeMail action="get" connection="testconn1" name="getMessages">
<cfExchangeFilter name="subject" value="message 1">
</cfexchangeMail>

<cfdump var="#getMessages#">

<!--- Close the connection. ---->
<cfexchangeConnection
action="close"
connection="testconn1">

<cfcatch>
   <cfoutput>#CFCATCH.message#</cfoutput>
</cfcatch>
</cftry>

You can download the source files using the download button.

Extract Text From PDF

Here is an Example showing the extraction of text from a pdf file, using ddx. The extracted text will be in the destination file.

<!--- The ddx file --->
<cfset ddxfile = Expandpath("doc_text.ddx")>
<!--- The source pdf file --->
<cfset sourcefile1 = Expandpath("pdf-file1.pdf")>
<!--- The destination file --->
<cfset destinationfile = Expandpath("ddx_result_doc_text.xml")>

<cfset inputStruct=StructNew()>
<cfset inputStruct.Doc1="#sourcefile1#">

<cfset outputStruct=StructNew()>
<cfset outputStruct.Out1="#destinationfile#">

<cfpdf action="processddx" ddxfile="#ddxfile#" inputfiles="#inputStruct#" outputfiles="#outputStruct#" name="ddxVar">

<cfoutput>The ddx operation was #ddxVar.Out1#</cfoutput><br>

<cfif #ddxVar.Out1# eq "successful">
   <cffile action="read" file="#destinationfile#" variable="filedata">
<cfdump var="#filedata#">
</cfif>

You can download the files using the download button.

URLEncodedFormat example

Following is an example to show the usage of the function "URLEncodedFormat".

Description: Generates an URL-encoded string. For example, it replaces spaces with %20, and non-alphanumeric characters with equivalent hexadecimal escape sequences.

Returns: A copy of a string, URL-encoded.

syntax: URLEncodedFormat(string [, charset ])

string: A string or a variable that contains a string.

charset: The character encoding in which the string is encoded. Optional.

The following list includes commonly used values:

* utf-8 * iso-8859-1 * windows-1252 * us-ascii * shift_jis * iso-2022-jp * euc-jp * euc-kr * big5 * euc-cn * utf-16

Here is a simple example:

<cfset enc = URLEncodedFormat("Adobe Cold Fusion 8")>
<br><cfoutput>The URLEncodedFormat of "Adobe Cold Fusion 8" is <br>"#enc#".
</cfoutput>

<br><cfset enc = URLEncodedFormat("JRUN&ColdFusion 8.0.1")>
<cfoutput>The URLEncodedFormat of "JRUN&ColdFusion 8.0.1" is <br>"#enc#".
</cfoutput>

Another example follows:

<h3>URLEncodedFormat Example</h3>
<cfif IsDefined("url.myExample")>
<p>The url variable url.myExample was passed from the previous link ...
its value is:
<br><b>"<cfoutput>#url.myExample#</cfoutput>"</b>
</cfif>
<p>This function returns a URL encoded string.
<cfset s = "My url-encoded string has special characters & other stuff">
<p> <A HREF = "urlencodedformat.cfm?myExample=<cfoutput>#URLEncodedFormat(s)#
</cfoutput>"
>Click me</A>

You can download the code using the download button.

cfchart embedded inside cfdocument

Example to show the usage of cfchart embedded inside cfdocument.

Description: Embed a cfchart inside a cfdocument. Note: In case of cfcharts, if you want to embed a cfchart inside a cfdocument, you can use the following formats for cfchart. 1. jpg 2. png Currently, format="flash" is NOT supported.

You should have the datasource cfdocexamples(this comes by default with the installation)

<!---The following example analyzes the salary data in the cfdocexamples
database and generates a bar chart showing average salary by department. The
body of the cfchartseries tag includes one cfchartdata tag to include data that
is not available from the query. --->


<cfdocument format="pdf">

<!--- Get the raw data from the database. --->
<cfquery name="GetSalaries" datasource="cfdocexamples">
SELECT Departmt.Dept_Name,
Employee.Dept_ID,
Employee.Salary
FROM Departmt, Employee
WHERE Departmt.Dept_ID = Employee.Dept_ID
</cfquery>

<!--- Use a query of queries to generate a new query with statistical data for each department.
AVG and SUM calculate statistics. GROUP BY generates results for each department. --->

<cfquery dbtype="query" name="DataTable">
SELECT
Dept_Name,
AVG(Salary) AS avgSal,
SUM(Salary) AS sumSal
FROM GetSalaries
GROUP BY Dept_Name
</cfquery>

<!--- Reformat the generated numbers to show only thousands. --->
<cfloop index = "i" from = "1" to = "#DataTable.RecordCount#">
      <cfset DataTable.sumSal[i] = Round(DataTable.sumSal[i]/1000)*1000>
<cfset DataTable.avgSal[i] = Round(DataTable.avgSal[i]/1000)*1000>
</cfloop>

<h1>Employee Salary Analysis</h1>
<!--- Bar graph, from Query of Queries --->
<cfchart format="jpg" xaxistitle="Department" yaxistitle="Salary Average">
<cfchartseries type="cone" query="DataTable" itemcolumn="Dept_Name" valuecolumn="avgSal">
    <cfchartdata item="Facilities" value="35000">
</cfchartseries>
   </cfchart>

</cfdocument>

You can download the example code using the download link.

cfdirectory single character wild card example

cfdirectory single character wild card example.

Here is an example to show the usage of cfdirectory filtering using single character wild card(?).

Description: cfdirectory manages interactions with directories.

Syntax

<cfdirectory
directory = "directory name"
action = "list|create|delete|rename"
filter = "list filter"
listInfo = "name|all"
mode = "permission"
name = "query name"
newDirectory = "new directory name"
recurse = "yes|no"
sort = "sort specification"
type = "file|dir|all">

Note: You can specify this tag's attributes in an attributeCollection attribute whose value is a structure. Specify the structure name in the attributeCollection attribute and use the tag's attribute names as structure keys.

<!--- List all files in the current directory based on the following filters:
       Get files whose filenames have only FOUR characters followed by the 'cfm' extension type. ex:test.cfm, fun1.cfm etc
--->

<cfset filters = "????.cfm">

<cfdirectory
   action="list"
   directory="#ExpandPath( '.' )#"
   filter="#filters#"
   listinfo="name"
   name="dirlist"
/>


<!--- Output file list. --->
<cfdump var="#dirlist#" label="Files with four chars">
<br>

<!--- Multiple filters:
       Get files whose filenames have only FOUR characters followed by the 'cfm' extension type. ex:test.cfm, fun1.cfm etc
       And
       Get files whose filenames have only THREE characters followed by the 'cfm' extension type. ex:fun.cfm
       And
       Get ALL files with .cfc extension. ex:app.cfc
--->

<cfset filters = "????.cfm|???.cfm|*.cfc">
<cfdirectory
   action="list"
   directory="#ExpandPath( '.' )#"
   filter="#filters#"
   listinfo="name"
   name="dirlist"
/>


<!--- Output file list. --->
<cfdump var="#dirlist#" label="Multiple Filters">
<br>

You can download the sample cfm file using the download button.

cfdirectory filters example

Example to show the usage of cfdirectory with filters.

Description: cfdirectory manages interactions with directories.

Syntax

<cfdirectory
directory = "directory name"
action = "list|create|delete|rename"
filter = "list filter"
listInfo = "name|all"
mode = "permission"
name = "query name"
newDirectory = "new directory name"
recurse = "yes|no"
sort = "sort specification"
type = "file|dir|all">

Note: You can specify this tag's attributes in an attributeCollection attribute whose value is a structure. Specify the structure name in the attributeCollection attribute and use the tag's attribute names as structure keys.

<!--- List all files in the current directory based on the following filters(single filter). --->
<cfset filters = "*.cfm">

<cfdirectory
   action="list"
   directory="#ExpandPath( '.' )#"
   filter="#filters#"
   listinfo="name"
   name="dirlist"
/>


<!--- Output file list. --->
<cfdump var="#dirlist#" label="All cfm files">
<br>

<!--- List all files in the current directory based on the following filters(multiple filter). --->
<cfset filters = "*.cfm|*.cfc">

<cfdirectory
   action="list"
   directory="#ExpandPath( '.' )#"
   filter="#filters#"
   listinfo="name"
   name="dirlist"
/>


<!--- Output file list. --->
<cfdump var="#dirlist#" label="cfm and cfc files">
<br>

cfdirectory example

Following is an example to show the usage of cfdirectory.

<!--- List all files in the current directory. --->
<cfdirectory
   action="list"
   directory="#ExpandPath( '.' )#"
   listinfo="name"
   name="dirlist"
/>


<!--- Output file list. --->
<cfdump var="#dirlist#" label="All Files">
<br>

cfftp example

Here is an Example to show the usage of cfftp.

cfftp lets users implement File Transfer Protocol operations. By default, cfftp caches an open connection to an FTP server. cfftp operations are usually of two types: 1. Establishing a connection 2. Performing file and directory operations

This example opens and verifies a connection, lists the files in a directory, and closes the connection.

<!--- Open a connection --->
<cfftp action = "open"
username = "user1"
connection = "myconn"
password = "mypwd"
server = "10.192.36.229"
stopOnError = "Yes">


<!--- Did the connection succeed... --->
<cfoutput>#cfftp.succeeded#</cfoutput>

<!--- List the files in a directory... --->
<cfftp action = "LISTDIR"
stopOnError = "Yes"
name = "ListFiles"
directory = "/"
connection = "myconn">


<cfoutput query = "ListFiles">
#name#<br>
</cfoutput>

<!--- Close a connection --->
<cfftp action = "close"
connection = "myconn"
stopOnError = "Yes">


<!--- Did the close succeed... --->
<cfoutput>#cfftp.succeeded#</cfoutput>

You can download the example code using the download link.

ArrayIsDefined Example

Here is an example to show the usage of ArrayIsDefined function in CF8.

This function determines whether an array element is defined.

Returns YES, if the array element is defined (exists). Returns NO, if the array element is NOT defined.

Function syntax: ArrayIsDefined(array, elementIndex)

<!--- Create a new array --->
<cfset MyArray = ArrayNew(1)>

<!--- Populate the array --->
<cfset MyArray[1] = "First Element">
<cfset MyArray[3] = "Third Element">

<!--- Display the contents of the array --->
<p>
Your array contents are:
<cfdump var="#MyArray#">
</p>

<cfoutput>
<!--- Check if the elements are defined. --->
<p>Does element 1 exist?
#ArrayIsDefined(MyArray, 1)#
<p>Does element 2 exist?
#ArrayIsDefined(MyArray, 2)#
<p>Does element 3 exist?:
#ArrayIsDefined(MyArray, 3)#</p>
</cfoutput>

Note: This function currently throws an error if the array is NOT initialized. So if the array is not yet created, you should surround the ArrayIsDefined code in a block. Also, the ArrayIsDefined function throws an error if you test for an element beyond the length of the array. To prevent this error, you can surround the test in the ArrayLen function.

You can download the cfm code using the download button.

More Entries

BlogCFC was created by Raymond Camden. This blog is running version 5.9.002. Contact Blog Owner