Janrow
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:\destinatio n\destfile _001.jpg">
Is there a way to do this with cffile or any other cf8 tag, and using the example with cffile above?
<cffile action="copy" source="sourcefile_001.jpg
Is there a way to do this with cffile or any other cf8 tag, and using the example with cffile above?
That should happen automatically.
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.
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.
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(fullP athToFile) >
<cfset newDate = createObject("java", "java.util.Date").init()>
<cfset javaFileObject.setLastModi fied(newDa te.getTime ())>
<cfset fullPathToFile = "c:\pathToYourFile.jpg">
<cfset javaFileObject = createObject("java", "java.io.File").init(fullP
<cfset newDate = createObject("java", "java.util.Date").init()>
<cfset javaFileObject.setLastModi
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(fullP athToFile) >
<cfset newDate = createObject("java", "java.util.Date").init()>
<cfset wasChanged = javaFileObject.setLastModi fied(newDa te.getTime ())>
<cfcatch type="java.lang.SecurityEx ception">
<cfset wasChanged = false >
</cfcatch>
</cftry>
<cfdump var="wasChanged = #wasChanged#">
<cftry>
<cfset fullPathToFile = "c:\pathToYourFile.jpg">
<cfset javaFileObject = createObject("java", "java.io.File").init(fullP
<cfset newDate = createObject("java", "java.util.Date").init()>
<cfset wasChanged = javaFileObject.setLastModi
<cfcatch type="java.lang.SecurityEx
<cfset wasChanged = false >
</cfcatch>
</cftry>
<cfdump var="wasChanged = #wasChanged#">
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\# newFilenam e#">
<cfset fullPathToFile = "c:\download\#newFilename# ">
<cfset javaFileObject = createObject("java", "java.io.File").init(fullP athToFile) >
<cfset newDate = createObject("java", "java.util.Date").init()>
<cfset javaFileObject.setLastModi fied(newDa te.getTime ())>
#newFilename# is the file to be copied and downloaded from the destination folder.
Jan
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\#
<cfset fullPathToFile = "c:\download\#newFilename#
<cfset javaFileObject = createObject("java", "java.io.File").init(fullP
<cfset newDate = createObject("java", "java.util.Date").init()>
<cfset javaFileObject.setLastModi
#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
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Yes, that works, and much simpler. Thanks!
Yes, the more complicated example would be for MX7. CF8 has some nice improvements over MX7!