Reputation: 331
I am trying to delete and recreate a test file, but am getting the error below. Can anyone tell me why this is happening?
Error:
ColdFusion could not delete the file e:\sample\test2.html for an unknown reason.
Code:
<cfif FileExists("e:\sample\test2.html")>
<cffile action="delete"file="e:\sample\test2.html"><br>
<p>deleted the file </p>
<cfelse>
<p>Sorry, can't delete the file - it doesn't exist.</p>
</cfif>
<cfset createObject("java", "java.lang.Thread").sleep(JavaCast("int", 90000))>
<cfsavecontent variable="HTML">
<cfinclude template="noticeEmail.cfm">
</cfsavecontent>
<cffile action="WRITE" attributes = "normal" nameconflict="Overwrite"
file="e:\sample\test2.html" output="#HTML#">
Upvotes: 3
Views: 5187
Reputation: 91
I was having the same problem, though none of the answers I found applied. Although my file passed the FileExists test, it still failed the actual delete. I was building the filename using variables:
<cfset sFilename = REQUEST.SubscriberWebFiles
& form.SubscriberID & "\Attachments\"
& qryAttachmentsTrash.AttachmentName>
I wrapped the code with a Trim:
<cfset sFilename = Trim(REQUEST.SubscriberWebFiles
& form.SubscriberID & "\Attachments\"
& qryAttachmentsTrash.AttachmentName)>
and the problem magically disappeared!
Upvotes: 1
Reputation: 331
You can use this code to delete an MS Word document with ColdFusion
<cfif FileExists( WordFilePath &".doc")>
<cffile action="delete"
file="#WordFilePath#.doc">
<cfelse>
Sorry, can't delete the file - it doesn't exist.
</cfif>
Upvotes: 0
Reputation: 23
Your web server/os might be holding onto the file. Can you try creating the file without opening it and see if you delete it then? So just create a file, and then delete it right away.
Upvotes: 0
Reputation: 473
The first thing I would check is permissions for the user that ColdFusion is running as.
I don't know how you're running CF or in what sort of environment you're on but assuming that this is a local development environment, are on a windows machine and are running ColdFusion as a service:
Other things that could cause errors, could another program have this file locked, what is happening with the file after it is created (is it only created through ColdFusion)
Also could you post the actual error that you are receiving?
If it's "Sorry, can't delete the file - it doesn't exist." Then this is due to the check you've got at the top to confirm if the file exists.
Finally the code that you've pasted above appears to have the first character missing from every line, it would be worth fixing this so that others can more easily copy and paste the code.
Upvotes: 2