1

I am using cffile to create a new file or update an existing file, depending on what the user requested. The request comes through a form from the previous procedure, so the code involving cffile looks like this:

<cfset thefile = "#form.dyn#">
<cfoutput>
<cfsavecontent variable = "testvar">
  #form.editor1#
</cfsavecontent>     
<cffile action = "write"
    file   = "/var/www/reports/#thefile#.cfm"
    output = "#testvar#">
</cfoutput>

When I am done writing to the file, I want to confirm to the user that this happened. For a new file I could use IsDefined to check that it is there. But I can't think of a way to check for an existing file that was updated. I considered a try/catch on the cffile, but the catch operates only if nothing seems to go wrong. If I don't get an error on the catch, can I assume everything is all right? I would prefer a direct check if possible. Does anyone have an idea?

Peter Boughton
  • 110,170
  • 32
  • 120
  • 176
Betty Mock
  • 1,373
  • 11
  • 23
  • 3
    Re *use IsDefined to check that it is there*. No, that is only for variables. For files, use `FileExists` -or- [`GetFileInfo`](http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-6d02.html) for existing files (CF9+). Though this seems like overkill. If the `cffile` action fails, CF will throw an error and you can take action in the `cfcatch` clause. – Leigh Jun 25 '13 at 19:43
  • Thanks Leigh for enlightening me about the FileExists or GetFileInfo. Guess I would have found this out if I had tried IsDefined. – Betty Mock Jun 26 '13 at 23:11

3 Answers3

5

You could use cfdirectory with the action="list" and filter="your-filename" to get the following information about the uploaded file:

If action = "list", cfdirectory returns the following result columns, which you can reference in a cfoutput tag:

  • name: Directory entry name. The entries "." and ".." are not returned.
  • directory: Directory that contains the entry.
  • size: Directory entry size.
  • type: File type: file, for a file; dir, for a directory.
  • dateLastModified: The date that an entry was last modified.
  • attributes: File attributes, if applicable.
  • mode: Empty column; retained for backward compatibility with ColdFusion 5 applications on UNIX.

Of interest to you is the dateLastModified column.

So you should be able to do something like:

<cfdirectory action="list" name="dirQuery" directory="C:/var/www/reports/" filter="#thefile#.cfm">

Then you can dump that result to see what information is available to you:

<cfdump var="#dirQuery#">

The dateLastModified column can be accessed like:

<cfoutput>#dirQuery.dateLastModified#</cfoutput>
Miguel-F
  • 13,450
  • 6
  • 38
  • 63
5

If <cffile> doesn't work, it'll tell you by throwing an exception. If it doesn't do that, you can safely assume it has worked. Don't over-engineer your app.

Adam Cameron
  • 29,677
  • 4
  • 37
  • 78
  • 1
    Of course, one must be clear about what "over-engineer" means. I am expecting helpless users, so I have to protect them to the extent possible. – Betty Mock Jun 26 '13 at 23:17
  • "Over-engineer" in this case is "writing unnecessary code" which is what you'd be doing if you wrote any more than zero code to deal with this. You are trying to solve an issue that is already dealt with for you. – Adam Cameron Jun 27 '13 at 05:24
2

Use CFDirectory to get the file's dateLastModified before you update the file and then again afterwards. If they are not the same, then it was updated.

Leigh
  • 28,765
  • 10
  • 55
  • 103
Ron Rattie
  • 71
  • 1