<!-- Template queries the database to get list of actors and checks for photos -->
<CFQUERY NAME="ACTORS" DATASOURCE="OWS">
select NameLast,NameFirst,ActorID from Actors
</CFQUERY>
<!--- Setup variables to be used in writing the file --->
<!--- Variable to hold output string, with column names in first line, terminated by CRLF --->
<CFSET TXTOUTPUT = '"NAMELAST","NAMEFIRST","ACTORID","PHOTOEXISTS","PHOTOFILE"'>
<!--- Set File Name to write string into, getting a temporary file name using the GetTempFile() Function--->
<CFSET OUTFILE = GetTempFile("C:\INETPUB\WWWROOT\OWS\CHAP35\FILES\","CHAP35")>

<!--- Check for the existence of the file.  If it exists, delete it --->
<CFIF FileExists(OUTFILE) is "Yes">
	<CFFILE ACTION="DELETE" FILE="#OUTFILE#">
</CFIF>

<!--- Write out the Header Line --->
<CFFILE ACTION="WRITE" FILE="#OUTFILE#" OUTPUT="#TXTOUTPUT#" ADDNEWLINE="YES">



<CFLOOP QUERY="ACTORS">
<!--- use the GetDirectoryFromPath() and GetBaseTemplatePath() functions to build
 part of the variable  based on currently executing template path --->
<CFSET PhotoFile_GIF=GetDirectoryFromPath(GetBaseTemplatePath()) & "FILES\ACTOR_#ACTORS.ACTORID#.GIF">
<CFSET PhotoFile_JPG=GetDirectoryFromPath(GetBaseTemplatePath()) & "FILES\ACTOR_#ACTORS.ACTORID#.JPG">
    <!--- Check for a GIF image --->
    <CFIF FileExists(PhotoFile_GIF)>
        <CFSET PhotoExists="YES">

        <CFSET PhotoFile=PhotoFile_GIF>
    <!--- Check for a JPG image --->
    <CFELSEIF FileExists(PhotoFile_JPG)>
        <CFSET PhotoExists="YES">
        <CFSET PhotoFile=PhotoFile_JPG>
    <!--- Otherwise --->
    <CFELSE>
        <CFSET PhotoExists="NO">
        <CFSET PhotoFile="">
    </CFIF>
    <CFSET TXTOUTPUT = '"#NAMELAST#","#NAMEFIRST#","#NumberFormat(ACTORID,'0000')#","#PHOTOEXISTS#","#PHOTOFILE#"'>
	<!--- Determine if file is to be written or appended to --->
	<CFIF NOT FileExists(OUTFILE)>
		<CFFILE ACTION="WRITE" FILE="#OUTFILE#" OUTPUT="#TXTOUTPUT#" ADDNEWLINE="YES">
	<CFELSE>
		<CFFILE ACTION="APPEND" FILE="#OUTFILE#" OUTPUT="#TXTOUTPUT#" ADDNEWLINE="YES">
	</CFIF>
</CFLOOP>

<!--- Read the file back in for display --->
<CFFILE ACTION="READ" FILE="#OUTFILE#" VARIABLE="TXTINPUT">

<HTML>
<HEAD>
	<TITLE>&lt;CFFILE&gt; Read/Write Demonstration</TITLE>
</HEAD>

<BODY bgcolor="white">
&lt;CFFILE&gt; Read/Write Demonstration<P>
<CFOUTPUT>
Information from the Actors table was written into the file located at #OUTFILE#.<P>
The contents of the file are shown below:
<HR>
<PRE>
#TXTINPUT#
</PRE>
</CFOUTPUT>
</BODY>
</HTML>
