I mentioned in my previous post with the same title that I used a renaming function to check if the file was finished and unlocked by the FTP transfer, so I could work with it. Well, after several tests using the renaming test, I was still getting lock up errors and my process was stopping.

After some research, I realized that our FTP server (WS_FTP) did not lock the file when writing to it, so I was able to rename it, and it would continue to upload to the same renamed file. Even when I moved the renamed file to a work directory, the file size was still being increased. So none of my test functions would work.

Instead of changing the FTP server settings – it is an old one installed in 2002 – I decided to install Filezilla Server. Wonderful, it locks the file and my test function now works.

Here is an updated checkFile function, it renames the file for work if it is unlocked.
<cffunction name="checkFile" access="remote" output="false" returntype="Any">
   <cfargument name="filePath" type="string" required="true" hint="full physical path" />
   <cfargument name="preStr" type="string" required="false" default="work-" />
   <cfset result = structNew() />
   <cftry>
      <cfset jFile = createobject("java", "java.io.File").init(arguments.filePath) />
      <cfif jFile.canWrite()>
         <cfset newFilePath = reverse(listRest(reverse(arguments.filePath), "\")) & "\" & arguments.preStr & listLast(arguments.filePath, "\") />
         <cffile action="rename" source="#arguments.filePath#" destination="#newFilePath#">
         <cfset result.status = true />
         <cfset result.content = newFilePath />
      </cfif>
      <cfcatch>
         <cfset result.status = false />
         <cfset result.content = "File #listLast(arguments.filePath, '\')# in use..." />
      </cfcatch>
   </cftry>
   <cfreturn result />
</cffunction>

Here is how you call the function:
<cfdirectory name="qDir" action="list" directory="#expandPath('data/')#" filter="*.zip" />
<cfloop query="qDir">
   <cfset filePath = qDir.directory & "\" & qDir.name />
   <cfset result = checkFile(filePath) />
   <cfif result.status>
      <!--- status is true: display message and process the file --->
      <cfoutput><br/>Processing #result.content#</cfoutput>
      <cfset processTheFile(result.content) />
   <cfelse>
      <cfoutput><br/>#result.content#</cfoutput>
   </cfif>
</cfloop>

Leave a Reply