ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Tom Dimock <t...@cornell.edu>
Subject Re: delete question
Date Wed, 14 Jun 2000 19:53:43 GMT
Kevin - If you don't mind adding to Ant, here is a task class definition
which will do what you want.

package org.apache.tools.ant.taskdefs;

import org.apache.tools.ant.*;

import java.io.*;

/**
 * Deletes a set of files defined by a pattern.
 *
 * @author Tom Dimock <a href="mailto:tad1@cornell.edu">tad1@cornell.edu</a>
 */
public class Deletefiles extends MatchingTask {

  /** Field */
  private File delDir;

  /** Field                        */
  private String verbose = "false";

  /**
   * Method setDeldir
   *
   * @param src
   */
  public void setDeldir(String src) {
    delDir = project.resolveFile(src);
  }

  /**
   * Method setVerbose
   *
   * @param verbose
   */
  public void setVerbose(String verbose) {
    this.verbose = verbose;
  }

  /**
   * Method execute
   *
   * @throws BuildException
   */
  public void execute() throws BuildException {

    if (delDir == null) {
      throw new BuildException("deldir attribute must be set!");
    }
    if (!delDir.exists()) {
      throw new BuildException("deldir does not exist!");
    }
    DirectoryScanner ds    = super.getDirectoryScanner(delDir);
    String[]         files = ds.getIncludedFiles();

    if (files.length > 0) {
      project.log("Deleting " + files.length + " files");
      for (int i = 0; i < files.length; i++) {
        File f = new File(delDir, files[i]);

        if (f.exists()) {
          if (f.isDirectory()) {
            project.log(
              "Directory: " + f.getAbsolutePath()
              + " cannot be removed with deletefiles.  Use Deltree instead.");
          }
          else {
            if ("true".equalsIgnoreCase(verbose.trim())) {
              project.log("Deleting: " + f.getAbsolutePath());
            }
            f.delete();
          }
        }
      }
    }
  }
}

----------------------------------------------------------------------------
       Tom Dimock  ----  Cornell University  ----  tad1@cornell.edu
"There go my people.  I must follow them, for I am their leader."  M. Gandhi

Mime
View raw message