ant-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Shawn Castrianni <>
Subject RE: delete directory structures older than X days
Date Tue, 16 Jun 2009 16:30:28 GMT
I needed something similar.  Both of our requirements requires the age of a file/directory
to be determined.  Therefore, the easiest approach I found was to write my own ANT condition.
 By writing a condition, you can use it inside an if statement or whatever else in ANT that
supports conditions.  Here is source code.  I am sure it can be improved.



public class FileAgeCondition implements Condition {
  protected String _file = null;
  protected float _age = -999.25F;  //In seconds
  protected boolean _olderThan = true;

  public boolean eval() throws BuildException
    if((_file == null) || (_file.length() == 0))
      throw new BuildException("file must be set");
    if(_age == -999.25F)
      throw new BuildException("age must be set");
    File file = new File(_file);
    long now = System.currentTimeMillis();
    long fileAge = file.lastModified();
    float testAge = _age * 1000.0F;
    boolean retVal;
      retVal = (now - fileAge) > testAge;
      retVal = (now - fileAge) < testAge;

  public void setFile(String file)
    _file = file;

  public void setAge(float age)
    _age = age;
Shawn Castrianni

-----Original Message-----
From: Sascha Ernst [] 
Sent: Tuesday, June 16, 2009 10:58 AM
Subject: delete directory structures older than X days


I googled the mailing list archive and found a matching solution based on javascript for ant

I'm using ant 1.7.1 and tried this to delete NBS-folders (NightlyBuildSystem) older than a
specific amount of time.

  <target name="delete.old.stuff"
          description="delete all NBS stuff which is older than a specific amount of time">

    <!-- start the timer -->
    <stopwatch name="timer.delete.old.stuff"/>

    <!-- first calculate the borderline timestamp -->
      <format property="borderline.timestamp" pattern="MM/dd/yyyy HH:mm aa"
              offset="${deletion.threadshold}" unit="day"/>

	<echo message="borderline.timestamp=${borderline.timestamp}" />
	<!-- now delete all NBS checkouts and stuff older than that boderline timestamp -->
	<echo message="delete ${NBS.dir.prefix}** in ${nbsdir} before ${borderline.timestamp}"
	<delete includeemptydirs="true" verbose="true">
	  <dirset dir="${nbsdir}" includes="${NBS.dir.prefix}**">	  
	    <date datetime="${borderline.timestamp}" when="before" checkdirs="true"/>

	<!-- now determine the  artifact folder -->
	<property name="artifact.dir" value="${builddir}/CruiseControl/artifacts/${projectname}"

	<echo message="artifact.dir=${artifact.dir}" />

	<!-- now delete all artifacts and stuff older than that boderline timestamp -->
	<echo message="delete **/**/* in ${artifact.dir} before ${borderline.timestamp}" />
	<delete includeemptydirs="true"  verbose="true">
	  <dirset dir="${artifact.dir}" includes="**/**/*">	  
	    <date datetime="${borderline.timestamp}" when="before"  checkdirs="true"/>
	<!-- print out timer and reset it -->
    <stopwatch name="timer.delete.old.stuff" action="total"/>

    <echo message="DONE : delete.old.stuff" />

Unfortunately this construction does not delete anything :-(

The console says this:
     [echo] borderline.timestamp=06/06/2009 17:32 PM
     [echo] delete NBS-run** in F:/NightlyBuildSystem before 06/06/2009 17:32 PM
     [echo] artifact.dir=F:\NightlyBuildSystem/CruiseControl/artifacts/Mailminder NBS
     [echo] delete **/**/* in F:\NightlyBuildSystem/CruiseControl/artifacts/Mailminder NBS
before 06/06/2009 17:32 PM
[stopwatch] [timer.delete.old.stuff: 0.844 sec]
     [echo] DONE : delete.old.stuff

Is there something I'm doing completely wrong?
Is there any possibility to delete such old stuff without using javascript anyway?

Thanks for your time and your help


///Sascha Ernst///

To unsubscribe, e-mail:
For additional commands, e-mail:

This e-mail, including any attached files, may contain confidential and privileged information
for the sole use of the intended recipient.  Any review, use, distribution, or disclosure
by others is strictly prohibited.  If you are not the intended recipient (or authorized to
receive information for the intended recipient), please contact the sender by reply e-mail
and delete all copies of this message.

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message