You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ant.apache.org by Tom Dimock <ta...@cornell.edu> on 2000/06/12 19:26:55 UTC

deletefiles

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.

<hr>
<h2><a name="deletefiles">Deletefiles</a></h2>
<h3>Description</h3>
<p>Deletes all files from the specified directory and its sub-directories.</p>
<p>It is possible to refine the set of files that are being deleted. This
can be
done with the <i>includes</i>, <i>excludes</i> and <i>defaultexcludes</i>
attributes. With the <i>includes</i> attribute you specify the files you
want to
have included in the deletion process by using patterns. The <i>exclude</i>
attribute is used to specify
the files you want to have excluded from the deletion process. This is also
done with patterns. And
finally with the <i>defaultexcludes</i> attribute, you can specify whether you
want to use default exclusions or not. See the section on <a
href="#directorybasedtasks">directory based tasks</a>, on how the
inclusion/exclusion of files works, and how to write patterns. The patterns
are
relative to the <i>deldir</i> directory.</p>
<h3>Parameters</h3>
<table border="1" cellpadding="2" cellspacing="0">
  <tr>
    <td valign="top"><b>Attribute</b></td>
    <td valign="top"><b>Description</b></td>
    <td align="center" valign="top"><b>Required</b></td>
  </tr>
  <tr>
    <td valign="top">deldir</td>
    <td valign="top">the directory to delete files from.</td>
    <td valign="top" align="center">Yes</td>
  </tr>
  <tr>
    <td valign="top">includes</td>
    <td valign="top">comma separated list of patterns of files that must be
      deleted. All files are in the current directory 
      and any sub-directories are deleted when omitted.</td>
    <td valign="top" align="center">No</td>
  </tr>
  <tr>
    <td valign="top">excludes</td>
    <td valign="top">comma separated list of patterns of files that must be
      excluded from the deletion list. No files (except default excludes)
are excluded when omitted.</td>
    <td valign="top" align="center">No</td>
  </tr>
  <tr>
    <td valign="top">defaultexcludes</td>
    <td valign="top">indicates whether default excludes should be used or not
      (&quot;yes&quot;/&quot;no&quot;). Default excludes are used when
omitted.</td>
    <td valign="top" align="center">No</td>
  </tr>
</table>
<h3>Examples</h3>
<pre>  &lt;deletefiles deldir=&quot;lib&quot; /&gt;</pre>
<p>deletes all files in the <code>/lib</code> directory.</p>
<pre>  &lt;deletefiles deldir=&quot;.&quot;
               include=&quot;**/*.bak&quot;
  /&gt;
</pre>
<p>deletes all files with the extension &quot;<code>.bak</code>&quot from
the current directory 
and any sub-directories.</p>
<hr>


As an aside - is the set of default excludes defined anywhere other than
the source?? I'm presuming that the definition found in class
DirectoryScanner is it.

    /**
     * Patterns that should be excluded by default.
     *
     * @see #addDefaultExcludes()
     */
    private final static String[] DEFAULTEXCLUDES = {
        "**/*~",
        "**/#*#",
        "**/%*%",
        "**/CVS",
        "**/CVS/*",
        "**/.cvsignore"
    };

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

Re: deletefiles

Posted by Tom Dimock <ta...@cornell.edu>.
At 07:59 PM 06/12/2000 +0200, you wrote:
>Hi Tom,
>
>How come you don't submit that code into Ant, so everyone can benefit from
>it?

Well, I'm not a committer, and the jakarta site says the way a developer
submits contributions is thriugh this mail list.  So I guess that I am
submitting it into Ant.  If I don't understand the process, and there is
something else I should be doing, please let me know!
----------------------------------------------------------------------------
       Tom Dimock  ----  Cornell University  ----  tad1@cornell.edu
"There go my people.  I must follow them, for I am their leader."  M. Gandhi

Re: Where Have All the Committers Gone?

Posted by James Duncan Davidson <ja...@eng.sun.com>.
on 6/12/00 12:35 PM, Arnout J. Kuiper at ajkuiper@planet.nl wrote:

> Yes, I intend to keep me busy with rewriting the core. But I'm partly
> waiting for the next version of the Ant-spec to arrive, because there were
> some issues in it which were very important for the rewrite.

I'll try to hit that in the next couple of days... Monday/Tuesday/Wednesday.

.duncan


Re: Where Have All the Committers Gone?

Posted by "Arnout J. Kuiper" <aj...@planet.nl>.
>I get the impression that Arnout is busy rewriting the core ... but I
>haven't seen any commits in a long time.

Lately I was very busy switching to a new employer (Sun), and attending 
JavaOne, etc. I have a huge backlog on my mail. I'm trying to catch up this 
week, so bear with me.

Yes, I intend to keep me busy with rewriting the core. But I'm partly 
waiting for the next version of the Ant-spec to arrive, because there were 
some issues in it which were very important for the rewrite.

Cheers for now,

   Arnout


Where Have All the Committers Gone?

Posted by Jason Sando <js...@think1up.com>.
It appears nobody is minding the store.  This particular patch as been
submitted, what, 5 times now in the last two months?  Each time lots of
discussion ensues about delete, copy, and move and then nothing happens.

Anybody listening?  Committers?  Anyone?  Somebody finally going to pick
this up?

I get the impression that Arnout is busy rewriting the core ... but I
haven't seen any commits in a long time.

-j

> -----Original Message-----
> From: Michael Powers [mailto:mpowers@BLaCKSMITH.com]
> Sent: Monday, June 12, 2000 11:13 AM
> To: ant-dev@jakarta.apache.org
> Subject: Re: deletefiles
>
>
> I think that's what we're talking about...
>
> If you don't have write privileges to CVS,
> this list is the proper channel, yes?
>
>   - Michael
>
> Jakob Ramskov wrote:
> >
> > Hi Tom,
> >
> > How come you don't submit that code into Ant, so everyone can
> benefit from
> > it?
> >
> > - Ramosa
> >
> > ----- Original Message -----
> > From: "Tom Dimock" <ta...@cornell.edu>
> > To: <an...@jakarta.apache.org>
> > Sent: Monday, June 12, 2000 7:51 PM
> > Subject: Re: deletefiles
> >
> > > Michael - Your code looks much like mine...
> > >
> > > public class Deletefiles extends MatchingTask {


Re: deletefiles

Posted by Michael Powers <mp...@BLaCKSMITH.com>.
I think that's what we're talking about...

If you don't have write privileges to CVS,
this list is the proper channel, yes?

  - Michael

Jakob Ramskov wrote:
> 
> Hi Tom,
> 
> How come you don't submit that code into Ant, so everyone can benefit from
> it?
> 
> - Ramosa
> 
> ----- Original Message -----
> From: "Tom Dimock" <ta...@cornell.edu>
> To: <an...@jakarta.apache.org>
> Sent: Monday, June 12, 2000 7:51 PM
> Subject: Re: deletefiles
> 
> > Michael - Your code looks much like mine...
> >
> > public class Deletefiles extends MatchingTask {

Re: deletefiles

Posted by Jakob Ramskov <co...@ramosa.com>.
Hi Tom,

How come you don't submit that code into Ant, so everyone can benefit from
it?

- Ramosa

----- Original Message -----
From: "Tom Dimock" <ta...@cornell.edu>
To: <an...@jakarta.apache.org>
Sent: Monday, June 12, 2000 7:51 PM
Subject: Re: deletefiles


> Michael - Your code looks much like mine...
>
> public class Deletefiles extends MatchingTask {



Re: deletefiles

Posted by Tom Dimock <ta...@cornell.edu>.
Michael - Your code looks much like mine...

public class Deletefiles extends MatchingTask {

    private File delDir;

    public void setDeldir(String src) {
        delDir = project.resolveFile(src);
    }

    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 {
                    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

Re: deletefiles

Posted by Michael Powers <mp...@BLaCKSMITH.com>.
> 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.");
*    }

}