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>