Link to home
Start Free TrialLog in
Avatar of Janrow
JanrowFlag for United States of America

asked on

How can I change the date and time attribute to the current date and time on a file after copying it with cffile

When copying a file with CFFILE, the original date and time needs to be changed to the current date.

<cffile action="copy" source="sourcefile_001.jpg" destination="c:\destination\destfile_001.jpg">

Is there a way to do this with cffile or any other cf8 tag, and using the example with cffile above?

Avatar of Plucka
Plucka
Flag of Australia image

That should happen automatically.
Avatar of Janrow

ASKER

> That should happen automatically.

Perhaps it should, but it does not. Never has. Using the cffile tag and options given in my example, the source file, once copied, always retains the original date.

I am copying the file from drive d (outside the webroot) to virtual website directory on drive c.

I'm using cf8 standard on win2003 r2 32bit server.
Avatar of _agx_
Afaik it does not change the timestamp of the file.  One way to change the date is to use java

<cfset fullPathToFile = "c:\pathToYourFile.jpg">
<cfset javaFileObject = createObject("java", "java.io.File").init(fullPathToFile)>
<cfset newDate = createObject("java", "java.util.Date").init()>
<cfset javaFileObject.setLastModified(newDate.getTime())>

Though if you want to be thorough you should catch any exceptions and check the returned value to verify the value really was changed.

<cftry>
      <cfset fullPathToFile = "c:\pathToYourFile.jpg">
      <cfset javaFileObject = createObject("java", "java.io.File").init(fullPathToFile)>
      <cfset newDate = createObject("java", "java.util.Date").init()>
      <cfset wasChanged = javaFileObject.setLastModified(newDate.getTime())>
      <cfcatch type="java.lang.SecurityException">
            <cfset wasChanged = false >
      </cfcatch>
</cftry>

<cfdump var="wasChanged = #wasChanged#">
Avatar of Janrow

ASKER

Hmm. I was hoping cffile has a parameter to set the date/time. I'm not much up to JavaScript, but I gave it a try, with your code and my variables for the fullpathtofile and got this error message:

Just in time compilation error
Invalid parser construct found on line 95 at position 61. ColdFusion was looking at the following text:
.
Invalid expression format. The usual cause is an error in the expression structure.
The last successfully parsed CFML construct was a CFSET tag occupying document position (95:1) to (95:6).

What might be wrong? Here's my cffile, with your code following it:

<cffile action="copy" source="#filename#" destination="c:\download\#newFilename#">

<cfset fullPathToFile = "c:\download\#newFilename#">
<cfset javaFileObject = createObject("java", "java.io.File").init(fullPathToFile)>
<cfset newDate = createObject("java", "java.util.Date").init()>
<cfset javaFileObject.setLastModified(newDate.getTime())>

#newFilename# is the file to be copied and downloaded from the destination folder.

Jan



The java code snippet you posted works fine. The error must be in some other part of the code.

But I realized there's a much easier way since you're using CF8.  Its a function.

FileSetLastModified
http://livedocs.adobe.com/coldfusion/8/htmldocs/functions_e-g_18.html#5177996
ASKER CERTIFIED SOLUTION
Avatar of _agx_
_agx_
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Janrow

ASKER

Yes, that works, and much simpler. Thanks!
Yes, the more complicated example would be for MX7.  CF8 has some nice improvements over MX7!