I am going crazy trying to find a solution for this issue.
Here is the code:
<!--- Rename PNG File to remove _page_1.png --->
<cfset fileOnServer = "#RootDirectory#\calendar\#uploadedImage#">
<cfif FileExists("#fileOnServer#")>
FileExists<br>
<cfset test = GetFileInfo("#fileOnServer#")>
<cfdump var="#test#">
<cfset fileName = "#test.name#">
<cfset newFileName = #Replace(fileName,'_page_1','')#>
<cfset fullNewFilePath = "#RootDirectory#\calendar\#newFileName#">
<cfoutput>fileName: #fileName#<br>newFileName: #newFileName#<br>fullNewFilePath: #fullNewFilePath#<br></cfoutput>
<cftry>
<cffile action="rename" destination="#fullNewFilePath#" source="#fileOnServer#" attributes="normal" nameconflict="overwrite">
<cfcatch>
<cfoutput>
<div style="text-align: left;">
Error occured....<br /><br />
Message: <b>#cfcatch.Message#</b><br />
Detail: <b>#cfcatch.Detail#</b><br />
Type: <b>#cfcatch.Type#</b><br />
</div>
</cfoutput>
<cfset ErrorOccurred = "Y">
</cfcatch>
</cftry>
</cfif>
When it runs, Here is the result:
FileExists
struct
canRead YES
canWrite YES
isHidden NO
lastmodified {ts '2020-05-15 09:36:39'}
name January_2019_page_1.png
parent C:\Inetpub\vhosts\MyDomain\calendar
path C:\Inetpub\vhosts\MyDomain\calendar\January_2019_page_1.png
size 501168
type file
fileName: January_2019_page_1.png
newFileName: January_2019.png
fullNewFilePath: C:\Inetpub\vhosts\MyDomain\calendar\January_2019.png
Error occured....
Message: Attribute validation error for tag CFFILE.
Detail: The value of the attribute source, which is currently C:\Inetpub\vhosts\MyDomain\calendar\January_2019_page_1.png, is invalid.
Type: Application
I have another cffile action="rename" that executes without issue earlier on the page. Also, this code will run fine if done on a separate page, just not on this page (where I need it to run).
My host is running ColdFusion 10 on Windows, if that helps any.
THANK YOU TO ANYONE WHO CAN HELP FIGURE THIS OUT!
~~Jennifer~~
* UPDATED TO SHOW WHAT FINALLY WORKED: *
<!--- Rename PNG File to remove _page_1.png --->
<cfset fileOnServer = "#RootDirectory#\calendar\#uploadedImage#">
<cfif FileExists("#fileOnServer#")>
<cfset RETRY_COUNT_MAX=10>
<cfset RETRY_SLEEP_MS=5000>
<cfloop index="retryCount" from="1" to="#RETRY_COUNT_MAX#">
<cftry>
<!--- Trying to repeat this code until it works --->
<cfset myFile = GetFileInfo("#fileOnServer#")>
<cfset fileName = "#myFile.name#">
<cfset newFileName = #Replace(fileName,'_page_1','')#>
<cfset fullNewFilePath = "#RootDirectory#\calendar\#newFileName#">
<cffile action="rename" destination="#fullNewFilePath#" source="#fileOnServer#" attributes="normal" nameconflict="overwrite">
<cfbreak>
<cfcatch>
<cfif retryCount GTE RETRY_COUNT_MAX><cfrethrow></cfif>
<cfthread action="sleep" duration="#RETRY_SLEEP_MS#" />
</cfcatch>
</cftry>
</cfloop>
</cfif>
There are a few possibilities I am aware of where that error can be misleading:
1) In some versions of CF, there was a bug where cffile reported an error with the source attribute when the real cause was an error with the destination attribute - eg a missing directory or filename conflict or lack of permissions on the destination. It sounds like you've ruled out most of those already though.
2) In some circumstances the Coldfusion Service user needs 'modify' permissions on the source directory for a rename action, even if it already has read + execute + write, and without it the result is this cryptic message. If that is the case then you should be able to prove it by copying the file to a different directory instead of renaming within the same directory.
3) I have seen issues before with file handle locks due to server processes monitoring certain directories for changes - including but not limited to AV/malware scanners, automated backup/robocopy scripts, and propagation of files to other nodes in a cluster. This can result in being unable to modify or rename a file for a few seconds after it is uploaded and the lock has been released - if this is the case and the server admin is outside of your control, then you should be able to work around it using a try/catch + loop to retry n times, with a short wait on each iteration - eg sleep(5000)
.