A user in the ColdFusion IRC room asked how he (I assume he) could use the cfschedule; tag such that when the task executed, it would save a different file per day.
By default, the output of the task run by cfschedule can only be saved to one file. However, you do not need to use this feature. You could instead simply schedule your page, and have the page itself save the output to a file. Let’s look at a complete example and then I’ll explain:
Where m is month number,
d is the day of the month,
and yyyy is - wait for it - year
<cfset filename = dateFormat(now(), "m_d_yyyy") & ".log">
<!--- full path is current folder --->
<cfset fullPath = expandPath("./#filename#")>
<!--- The content --->
Dell customer support is #randRange(99,100)#% bad.
<cffile action="append" file="#fullPath#" output="#content#">
We start by creating a file name. We use the dateFormat function to generate a name based on the current date. We then simply append “.log” to it. Since cffile requires a full path, I create a variable called fullPath. I use expandPath to simply tell it to use the current directory. (As a side note, you should try, as much as possible, to never use “real” paths in your code. It makes your code much more portable if you do not.)
Once I have a filename, I generate the content. Obviously, this portion will be different. I use the cfsavecontent tag to save all the output into a variable.
Then all we do is write the file out. Notice I used “append”. This way, if the scheduled event accidently runs twice, you don’t overwrite your old output. This isn’t the only thing you can do of course. You could overwrite the old file. You could also do a fileExists on fullPath, and throw an error. (Of course, since it is a scheduled event, you would probably want to mail yourself.) You could also generate a new file name by appending a number to the file name.
p.s. I’ve been doing a lot of blog entries lately will little tips like this. Some of which are pretty simple, but I assume my lesser-skilled readers will find them helpful. If not, let me know.