nAnt Deleting files older than 7 days old

拈花ヽ惹草 提交于 2019-12-04 12:14:24

问题


I would like to create a target that cleans log files older than 7 days old in a specific folder. I get an error when I try to put in a "date" element inside a fileset. How can I go about this?

<delete>
    fileset basedir="${StageIISRoot}/MySite/App_Data/ErrorLog">
        <date datetime="${datetime::now() - timespan::from-days(7)}" when="before"/>
        <include name="*.xml" />
    </fileset>
</delete>

回答1:


I don't see any documentation for using the "date" element. You might consider something like this:

<fileset id="thelogs" basedir="${StageIISRoot}/MySite/App_Data/ErrorLog">
    <include name="*.xml" />
</fileset>

And then reference that fileset later in a loop that checks the file date and deletes:

<foreach item="File" property="filename">
    <in>
        <items refid="thelogs" />
    </in>
    <do>
        <if test="${timespan::get-days(datetime::now() - file::get-last-write-time(filename)) >= 7}">
            <delete file="${filename}" />
        </if>
    </do>
</foreach>



回答2:


What about something like:

<tstamp>
    <format property="last.week" pattern="MM/dd/yyyy hh:mm" locale="en,UK" offset="-7" unit="day"/>
</tstamp>
<echo>Delete backups before ${last.week}</echo>
<delete>
    <fileset dir="${dst.dir}">
        <date datetime="${last.week}" when="before"/>
    </fileset>
</delete>

It seems to work for me :-)



来源:https://stackoverflow.com/questions/1820722/nant-deleting-files-older-than-7-days-old

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!