TreeDeleteTF

Delete files and directories recursively.

Task package:org.schmant.task.base
Java package:org.schmant.task.io
Category:I/O tasks
Since:0.5
EntityFS-aware?Yes*
Implements:ActionTaskFactory
See also:DeleteTF
RecursiveActionTF

Description:

Delete files and directories recursively from one or more directory hierarchies.

Required properties

Properties

deleteRoottop

Should the root directories (the directories added to the sources property) also be deleted? The default behavior is to only delete their contents.

Setter method:
setDeleteRoot(boolean dr)
parameters:
dr – Should the root directory/directories be deleted?
Default value:
false (The root directory is not deleted.)
logFootertop

The message that is logged to info level after the task has been successfully run.

Setter method:
setLogFooter(String s)
parameters:
s – The footer message.
Default value:
Empty (no footer message is logged.)
See also:
logHeader
logHeadertop

The message that is logged to info level before the task is run.

Setter method:
setLogHeader(String s)
parameters:
s – The header message.
Default value:
A task class specific message.
See also:
logFooter
preserveAttributestop

Should attributes of the source entities, such as their latest modification times, be copied to the target entities?

Setter method:
setPreserveAttributes(boolean b)
parameters:
b – Should attributes be preserved?
Default value:
false (attributes are not preserved)
reportLeveltop

This property is used to change the Report level for all task created by this task factory. The report level is changed for the thread running the task when the it is run, and is restored to its previous level when the it is done.

Setter method:
setReportLevel(Level l)
Set the report level
parameters:
l – The new report level.
sources (required)top

A collection of directories to delete recursively.

There are two ways that EntityFilter:s can be used to exclude files or directories from being deleted:

  1. Using a DirectoryView as source: This task will only delete files and directories that match the view's filter, and it will only delete directories without child entities. Furthermore, it will only recurse down into child directories that match the filter.
  2. Using a DirectoryAndFilter as source: This task will only delete files and directories that match the view's filter, and it will only delete directories without child entities. It will however recurse down into all child directories of a processed directory.

See the examples below

Setter method:
addSource(Object o)
Add one or several sources.
parameters:
o – A source object or an array or collection of source objects.
Interpreted by InterpretAsDirectoryStrategy.
Setter method:
addSources(Object o)
Add one or several sources.
parameters:
o – A source object or an array or collection of source objects.
Interpreted by InterpretAsDirectoryStrategy.
Setter method:
clearSources()
Discard all sources.
Setter method:
setSource(Object o)
Set one or several sources and discard previously set sources.
parameters:
o – A source object or an array or collection of source objects.
Interpreted by InterpretAsDirectoryStrategy.
Setter method:
setSources(Object o)
Set one or several sources and discard previously set sources.
parameters:
o – A source object or an array or collection of source objects.
Interpreted by InterpretAsDirectoryStrategy.
traceLoggingtop

If trace logging is enabled for a task, it reports its configuration before it is run.

Trace logging may also be enabled globally for all tasks by calling TraceMode.setTraceMode(boolean).

Setter method:
setTraceLogging(boolean b)
Enable or disable trace logging.
parameters:
b – Enable trace logging?

Examples

Example 1

Delete all files and subdirectories of d1. d1 itself is not deleted.

new TreeDeleteTF(). addSource(d1).run();

Example 2

Delete all .svn directories in d1's directory hierarchy.

// Note1: The SuperParentOrFilter matches an entity if any of its parent // entities matches the filter. // // Note2: Create the SuperParentOrFilter to include the tested entity in the // tests. Otherwise the .svn directory itself would not be deleted. new TreeDeleteTF(). addSource( new DirectoryAndFilter(d1, new SuperParentOrFilter( DirectoryFilter.FILTER.and( new EntityNameFilter(".svn")), true))).run();

Example 3

This is an alternative way of deleting the .svn directories compared to the previous example. Instead of using the SuperParentOrFilter with the TreeDeleteTF, it uses the RecursiveActionTF to define a new TreeDeleteTF task for each .svn directory found.

// Note: It may seem a little dangerous to start a recursive deleter while // we're iterating over the directory hierarchy with another iterator. It should // work fine, though, since we instruct RecursiveActionTF to use a depth-first // iterator. new RecursiveActionTF(). setDepthFirstIterator(true). addSource( new DirectoryAndFilter(d1, DirectoryFilter.FILTER.and( new EntityNameFilter(".svn")))). setTaskFactory( new TreeDeleteTF(). setDeleteRoot(true)).run();

Example 4

This is yet another way of deleting the .svn directories (see the two previous examples). This time, a FilteringIterator is used.

import org.entityfs.util.itr.FilteringIterator import org.entityfs.util.* import org.entityfs.util.filter.entity.* import TreeDeleteTF // Note1: Just like in the previous example, it is important that we use // a depth-first iterator. // // Note2, a gotcha: It is not possible to call the Entities.delete method from a // JavaScript, since "delete" is a reserved word there. Use the method // Entities.deleteEntity instead (it does exactly the same thing). def itr = new FilteringIterator( Directories.getDepthFirstIterator(d1), // Groovy lets us combine the filters using & DirectoryFilter.FILTER & new EntityNameFilter(".svn")) while(itr.hasNext()) { dv = itr.next() new TreeDeleteTF(). addSource(dv).run() Entities.deleteEntity dv }


* An EntityFS-aware task is implemented using EntityFS. This means that it uses the filter settings of DirectoryView:s and also that it often can work with other file system implementations than File-based, such as the RAM file system.