日期比较2个文件 - 查看文件是否需要更新

时间:2013-09-04 10:39:48

标签: coldfusion railo

如果中央文件夹中的文件较新,我想更新文件。

这是我到目前为止所做的事情,但是随着月份的变化似乎无法正常工作 - 几天和几分钟都可以正常工作。

<cfdirectory action="list" directory="#baseViewStackDir#" filter="#viewStackFileName#" name="base_fileInfo">
<!--- end --->
<cfset myViewStackDir = ExpandPath('/designer/app') />
<cfdirectory action="list" directory="#myViewStackDir#" filter="#viewStackFileName#" name="target_fileInfo">
<cfset copy = false />
<cfif DateCompare(base_fileInfo.DATELASTMODIFIED,target_fileInfo.DATELASTMODIFIED,"yyyy") GT 0 >
  <cfset copy = true />
<cfelseif DateCompare(base_fileInfo.DATELASTMODIFIED,target_fileInfo.DATELASTMODIFIED,"m") GT 0>
  <cfset copy = true />
<cfelseif DateCompare(base_fileInfo.DATELASTMODIFIED,target_fileInfo.DATELASTMODIFIED,"d") GT 0>
  <cfset copy = true />
<cfelseif DateCompare(base_fileInfo.DATELASTMODIFIED,target_fileInfo.DATELASTMODIFIED,"h") GT 0>
  <cfset copy = true />
<cfelseif DateCompare(base_fileInfo.DATELASTMODIFIED,target_fileInfo.DATELASTMODIFIED,"n") GT 0>
  <cfset copy = true />
<cfelseif DateCompare(base_fileInfo.DATELASTMODIFIED,target_fileInfo.DATELASTMODIFIED,"s") GT 0>
  <cfset copy = true />
</cfif>
<cfif copy EQ true>
  <cffile action="copy" source="#baseViewStackDir##viewStackFileName#" destination="#myViewStackDir#"/>
</cfif>
<script type="text/javascript">
  console.log("swf updated=<cfoutput>#copy# #base_fileInfo.DATELASTMODIFIED# - #target_fileInfo.DATELASTMODIFIED# ....#DateCompare(base_fileInfo.DATELASTMODIFIED,target_fileInfo.DATELASTMODIFIED,'m')#</cfoutput>");
</script>

任何人都可以发现我的错误吗?

1 个答案:

答案 0 :(得分:1)

正如彼得和我在评论中提到的那样,你可以将整个剧本改为这个

<cfif base_fileInfo.DateLastModified GT target_fileInfo.DateLastModified>
  <cffile action="copy" source="#baseViewStackDir##viewStackFileName#" destination="#myViewStackDir#"/>
</cfif>

<script type="text/javascript">
console.log("swf updated=<cfoutput>#copy# #base_fileInfo.DATELASTMODIFIED# - #target_fileInfo.DATELASTMODIFIED# ....#DateCompare(base_fileInfo.DATELASTMODIFIED,target_fileInfo.DATELASTMODIFIED,'m')#</cfoutput>");
</script>

如果您正在尝试 autoversion 您的js / css文件,您可以使用类似的内容来读取文件的修改日期并将该日期附加到js / css网址

<cffunction Name="autoversion" access="public" returntype="string" output="false">
  <cfargument Name="filepath" type="string" required="yes">
  <cfset var fileDate = createObject("java","java.util.Date").init(createObject("java","java.io.File").init('C:\railo\webapps\railo\website\bootstrap\js'&Replace(arguments.filepath, '/', '\', 'ALL')).lastModified())>
  <cfreturn 'https://www.mydomain.com/'&arguments.filepath&'?d='&DateFormat(fileDate, 'mmddyyyy')&TimeFormat(fileDate, 'hh')>
</cffunction>

<cfset autoversion('/bootstrap.js')>