1

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>
  • 4
    Error message for file operations are often misleading in CF (broad exceptions in Java not exactly helping). Most likely your source file is still locked when trying to rename/move it. Or the destination file already exists. Or your disk is full. Start debugging by copying the source file instead of renaming/moving it and see what happens. It you can narrow it down to a file lock, the real fun starts. What's the cause of the lock? An open file handle? Another thread maybe? You might want to share the code you run before trying to rename (where does the source file come from?). – Alex May 15 '20 at 22:40
  • Try to embrace every code handling with the files ( reading/renaming ) etc with cflock. – AndreasRu May 16 '20 at 14:05
  • You have other code on page? Does this code happen to exist in loop or cfc where there could be other conflicting variables? Dump out cfcatch as there may be useful info. – Dan Roberts May 17 '20 at 02:30
  • I have tried running the code with the cffile tags enclosed in a cflock tag and get the same error. I have tried copying (instead of renaming) the file -- same error. If the destination file exists, I have overwrite enabled, so that shouldn't matter, should it? Also, the file needs to exist in order to rename it, correct? This code is not in a loop or cfc. I can't dump the cfcatch because my host has that function disabled. I am thinking that the file lock may be the problem but don't really know how to troubleshoot that. I will keep working at it... – Jennifer Ayers May 18 '20 at 11:08
  • The file may be corrupt. Can you try a different file in the same source path? Does the folder itself have permission for ColdFusion to access it? – volume one May 18 '20 at 11:13
  • The file is not corrupt as it can be renamed if the code is run on a separate page. (I cannot rename any of the existing files in the directory without getting this error; it happens with every file rename/copy attempt that is run on this particular page.) There is no problem with permissions. – Jennifer Ayers May 26 '20 at 14:58

1 Answers1

0

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).

Sev Roberts
  • 1,295
  • 6
  • 7