Listing 1.
<HTML>
<HEAD>
<TITLE>Example CFLOCK</TITLE>
</HEAD>
<BODY>
<!--- open CFLOCK block --->
<CFLOCK TIMEOUT="5" NAME="Impeachment">
<CFTRANSACTION>
<CFQUERY NAME="Dump" DATASOURCE="cfsnippets">
UPDATE EMPLOYEES
SET Email = 'FIRED'
WHERE Department = 'Slacker'
</CFQUERY>
<CFQUERY NAME="Fetch" DATASOURCE="cfsnippets">
SELECT * FROM EMPLOYEES
</CFQUERY>
</CFTRANSACTION>
We fired:
<UL>
<CFOUTPUT QUERY="Fetch">
<CFSWITCH EXPRESSION="Department">
<CFCASE VALUE="Slacker">
#Fetch.LastName#.<BR>
</CFCASE>
<CFDEFAULTCASE>
Bill Clinton.<BR>
</CFDEFAULTCASE>
</CFSWITCH>
</CFOUTPUT>
</UL>
</CFLOCK><!--- close try block --->
</BODY>
</HTML>
Listing 2.
<HTML>
<HEAD>
<TITLE>Example CFTRY/CFCATCH</TITLE>
</HEAD>
<BODY>
<CFTRY> <!--- open try block --->
I see the train is coming...
<!--- condition for exception --->
<CFIF 1 IS 2>
<!--- throw exception &
message --->
<CFTHROW MESSAGE="We have
a problem!">
<CFELSE>
<H2>All Aboard!</H2>
</CFIF>
<!--- catch exception --->
<CFCATCH TYPE="application">
<H2>The train appears to have derailed</H2>
<!--- display custom message
thrown above --->
<CFOUTPUT>#CFCATCH.message#</CFOUTPUT>
</CFCATCH>
</CFTRY><!--- close try block --->
</BODY>
</HTML>
Listing 3.
<HTML>
<HEAD>
<TITLE>Example CFSWITCH</TITLE>
</HEAD>
<BODY>
<CFQUERY NAME="Get" DATASOURCE="cfsnippets">
SELECT * FROM EMPLOYEES</CFQUERY>
<CFOUTPUT QUERY="Get">
<!--- this value populated by query --->
<CFSWITCH EXPRESSION="#Get.Department#">
<!--- first case to compare with the expression
in the CFSWITCH
tag --->
<CFCASE VALUE="Administration">
#FirstName# #LastName# gets
2 hours for lunch
<BR>
</CFCASE>
<!--- next case to compare with the expression
in the CFSWITCH
tag --->
<CFCASE VALUE="Accounting">
#FirstName# #LastName# gets
1 hour for lunch
<BR>
</CFCASE>
<!--- next case to compare with the expression
in the CFSWITCH
tag --->
<CFCASE VALUE="Engineering">
#FirstName# #LastName# gets
no lunch
<BR>
</CFCASE>
<!--- default case catches the rest --->
<CFDEFAULTCASE>
#FirstName# #LastName# is out
to lunch
<BR>
</CFDEFAULTCASE>
</CFSWITCH>
</CFOUTPUT>
</BODY>
</HTML>
Listing 4.
<HTML>
<HEAD>
<TITLE>Example DirectoryExists()</TITLE>
</HEAD>
<BODY>
<!--- this variable could have been passed
by
a user as
part of a form submission --->
<CFSET newDir = "C:\Inetpub\wwwroot\CFDOCS">
<!--- the DirectoryExists function is used
here
in conjunction
with the conditional to
verify that
the directory exists before
action takes
place --->
<CFIF DirectoryExists("#newDir#") IS "Yes">
<CFDIRECTORY
ACTION="LIST"
DIRECTORY="#newDir#"
NAME="DirList"
SORT="Type ASC, Name ASC">
<CFOUTPUT QUERY="DirList">
#DirList.Name# - #DirList.Type# - #DirList.Size#
<BR>
</CFOUTPUT>
<CFELSE>
Sorry, that Directory doesn't
exist.
</CFIF>
</BODY>
</HTML>
Listing 5.
<HTML>
<HEAD>
<TITLE>Example Encryption</TITLE>
</HEAD>
<BODY>
<!--- form processing --->
<CFIF IsDefined("Post")>
<!--- encrypt the string passed by form
--->
<CFSET encrypted = #Encrypt(myString,Key)#>
<FORM ACTION="crypto.cfm"
METHOD="POST"
ENCTYPE="application/x-www-form-urlencoded">
<!--- display encrypted string in form
field --->
<CFOUTPUT><INPUT TYPE="Text" NAME="myString"
VALUE="#encrypted#"
SIZE="40" MAXLENGTH="50"><BR>
Decrypted using the Key: "#form.Key#"<BR>
<!--- undo the encryption using same key
--->
is "#Decrypt(encrypted,form.Key)#"
</CFOUTPUT><BR>
<INPUT TYPE="Submit" NAME="Back"
VALUE="Try Another?">
</FORM>
<!--- form entry front end --->
<CFELSE>
<FORM ACTION="crypto.cfm"
METHOD="POST"
ENCTYPE="application/x-www-form-urlencoded">
<INPUT TYPE="Text" NAME="myString"
VALUE="Type Text Here"
SIZE="40" MAXLENGTH="50"><BR>
<INPUT TYPE="Text" NAME="Key"
VALUE="EncryptionKey"
SIZE="20" MAXLENGTH="20"><BR>
<INPUT TYPE="Submit" NAME="Post"
VALUE="Encrypt This String">
</FORM>
</CFIF>
</BODY>
</HTML>
Listing 6.
<HTML>
<HEAD>
<TITLE>EXAMPLE GetTickCount</TITLE>
</HEAD>
<BODY>
<!--- set start time --->
<CFSET begMark = GetTickCount()>
<CFQUERY NAME="Get" DATASOURCE="cfsnippets">
SELECT * FROM EMPLOYEES</CFQUERY>
<!--- set ending time --->
<CFSET endMark = GetTickCount()>
<!--- convert difference to seconds --->
<CFSET Diff = (endMark - begMark)/1000>
<!--- if execution time exceeds pre-defined
limit, send
e-mail notice to sysop --->
<CFIF Diff GT 3>
<CFMAIL TO="sysop@domain.com"
FROM="WebWatcher@domain.com"
SUBJECT="Slow Query Warning"
SERVER="Mail.ISP.NET">
WARNING: QUERY "Get" on gettick.cfm has exceeded
reasonable response threshold (3 seconds).
Execution time: #Diff# seconds</CFMAIL>
</CFIF>
<CFOUTPUT QUERY="Get">
#LastName#, #FirstName# - #Department#<BR>
</CFOUTPUT>
</BODY>
</HTML>
Listing 7.
<HTML>
<HEAD>
<TITLE>Example REFindNoCase</TITLE>
</HEAD>
<BODY>
<!--- read a file into a variable --->
<CFFILE ACTION="READ"
FILE="D:\Inetpub\wwwroot\switch.cfm"
VARIABLE="myPage">
<!--- strip all carriage returns --->
<CFSET myString = StripCR("#myPage#")>
<!--- regular expression: "find the substring
beginning
with '<' then optionally
containing
'/' followed by 'Body' --->
<CFSET regex = "</?BODY">
<!--- find the 1st instance start position
--->
<CFSET firstPos =
REFindNoCase('#regex#','#myString#')>
<CFSET i = firstPos + 1>
<!--- find the 2nd instance start position
note: the
1st result was incremented
by 1 and then
passed as the starting
position for
the 2nd search--->
<CFSET secondPos =
REFindNoCase('#regex#','#myString#',#i#)>
<CFOUTPUT>
The 1st instance of target (case-insensitive)
begins at position: #firstPos#
<P>
The 2nd instance of target (case-insensitive)
begins at position: #secondPos#
</CFOUTPUT>
</BODY>
</HTML>