ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Michael Powers <mpow...@BLaCKSMITH.com>
Subject Re: deletefiles
Date Mon, 12 Jun 2000 17:41:42 GMT
> In my use of Ant, I have frequently had the need to delete a set of files
> based on some wildcard specifications.  To do this, I have written a new
> task definition for "deletefiles".  I have included the documentation
> below.  Would there be interest in this as an addition to the Ant standard
> tasks?  I'd be glad to contribute it.

I just joined this list, specifically so I could find
out how to integrate my spiffy new "Clean" task that 
can delete files recursively with include/exclude filters.

Looking at the archives index, this appears to be a common
problem that everyone is duplicating effort to solve.

(A lot of those posts appear to be patches - I'm wondering
why they're not in CVS.)

So what's the best way to solve this?  I'd simply like to 
see the Delete task extended to do this if the file 
specified is a directory.  See attached for source.

This is a backwards-compatible solution.  (If anyone is
already using Delete with a directory, they arguably want 
to delete the directory, since the current error message 
directs you to use Deltree.  This becomes the default
behavior.)

  - Michael

/**
 * Deletes a single file.  If a directory is specified,
 * it is recursively scanned to delete all files matching
 * the specified pattern.
 *
 * @author stefano@apache.org
 */

public class Delete extends MatchingTask {

    private File f;

    public void setFile(String File) {
        f = project.resolveFile(File);
    }

    public void execute() throws BuildException {

        if (f.exists()) {
            if (f.isDirectory()) {
*                project.log("Deleting from directory: " + f.getAbsolutePath());
*                deleteFromDirectory();
            } else {
                project.log("Deleting: " + f.getAbsolutePath());
                f.delete();
            }
        }
    }

*    private void deleteFromDirectory()
*    {
*        DirectoryScanner scanner = getDirectoryScanner( f );
*        scanner.scan();
*
*        File file;
*        String[] arr = scanner.getIncludedFiles();
*        for ( int i = 0; i < arr.length; i++ )
*        {
*            file = new File( f, arr[i] );
*            if ( file.exists() )
*            {
*                file.delete();
*            }
*        }
*        project.log("Deleted " + arr.length + " files.");
*    }

}

Mime
View raw message