nAnt Deleting files older than 7 days old
Asked Answered
E

2

6

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>
Entozoic answered 30/11, 2009 at 16:6 Comment(0)
G
9

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>
Glib answered 30/11, 2009 at 19:27 Comment(1)
I get an error saying "The source directory MYPATH/thelogs doesn't existEntozoic
P
5

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 :-)

Pavlov answered 9/2, 2011 at 17:32 Comment(1)
The date format needs a small change: <format property="last.week" pattern="MM/dd/yyyy hh:mm aa" offset="-7" unit="day"/> Then it worked for me as expected.Morry

© 2022 - 2024 — McMap. All rights reserved.