Copy link to clipboard
Copied
I am trying to compare the date of a file with the current time using this code
<cfset myPath = "C:\path\to\myfile.txt" >
<cfset myExists=Fileexists(myPath)>
<cfSet TIMEOUT="#DateAdd("h", "-6", Now() )#" >
<cfset myFile = CreateObject("java", "java.io.File")>
<cfset myFile.init(myPath)>
<cfset last_modified = myFile.lastModified()>
<cfif (TIMEOUT lt last_modified) or (not myExists)>
if myPath does not exist or it is at least 6 hours old regenerate the file
</cfif>
My problem is the DateAdd function creates the date/time in a {ts xxx} format while the .lastModified function creates a time stamp in a millisecond format.
What function do I use to compare these two different time stamps?
You might have better luck starting with cfdirectory and the datelastmodified field. Here are some snippets for one of my programs that does date based file maintenance.
ThisDate1 = DateAdd("d", -90, now());
<cfdirectory name="AllFiles" directory="#Dir1#">
<cfif AllFiles.recordcount gt 0>
<cfquery name="OldFiles" dbtype="query">
select name from AllFiles
where datelastmodified < #ThisDate1#
</cfquery>
etc
Copy link to clipboard
Copied
You might have better luck starting with cfdirectory and the datelastmodified field. Here are some snippets for one of my programs that does date based file maintenance.
ThisDate1 = DateAdd("d", -90, now());
<cfdirectory name="AllFiles" directory="#Dir1#">
<cfif AllFiles.recordcount gt 0>
<cfquery name="OldFiles" dbtype="query">
select name from AllFiles
where datelastmodified < #ThisDate1#
</cfquery>
etc
Copy link to clipboard
Copied
What function do I use to compare these two different time
stamps?
As mentioned, it is probably simpler to work with the results of cfdirectory.
But to answer your question, lastModified is an epoch date. You could
A) Convert it to a datetime object. Then compare the two dates.
B) Use the undocumented getTime() function, to get the milliseconds from your date object ie #now().getTime()#
There are also a few gotchas with epochs..
http://www.brooks-bilson.com/blogs/rob/index.cfm/2007/10/11/Some-Notes-on-Using-Epoch-Time-in-ColdFusion
Copy link to clipboard
Copied
My problem is the DateAdd function creates the date/time in a {ts xxx} format while the .lastModified function creates a time stamp in a millisecond format.
What function do I use to compare these two different time stamps?
Have a look at CF's date-oriented functions:
http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-6986.html
When doing so, bear in mind your requirement (the key word of which I've highlighted).
--
Adam
Copy link to clipboard
Copied
When doing so, bear in mind your requirement (the key word
of which I've highlighted).
Yes, I imagine some of the date functions probably would work with an epoch. But it seems like the long way around. Unless you really need to create a File object.
In CF8, cfdirectory + filter would give you the file modified date. Granted as a string. But it should work with date functions (en locale).
<cfdirectory directory="c:\pathTo" filter="someFile.txt" name="result" />
<cfif NOT result.recordCount OR dateDiff("h", result.dateLastModified, now()) gt 6>
regenerate the file
</cfif>
For CF9, I believe you could get the modified date from the GetFileInfo() function.
-Leigh
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more