Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I test to see that a directory is empty in ANT?

Tags:

directory

ant

How can one test to see that a directory is empty in ant?

like image 842
leeand00 Avatar asked Jun 15 '10 18:06

leeand00


People also ask

How do you check if a directory is empty or not?

File. list() is used to obtain the list of the files and directories in the specified directory defined by its path name. This list of files is stored in a string array. If the length of this string array is greater than 0, then the specified directory is not empty.

What is an empty directory?

An empty directory contains no files nor subdirectories. With Unix or Windows systems, every directory contains an entry for “ . ” and almost every directory contains “ .. ” (except for a root directory); an empty directory contains no other entries.


1 Answers

You can use the pathconvert task to do that, with the setonempty property.

<pathconvert refid="myfileset"
             property="fileset.notempty"
             setonempty="false"/>

will set the property fileset.notempty only if the fileset those refid is myfileset is not empty.

You just have to define myfileset with your directory, and no excludes do get a directory empty test:

<fileset dir="foo/bar" id="myfileset"/>

See this example for a use case:

use the setonempty attribute of pathconvert, with the value "false". This way, if the fileset is empty, the property will not be set. this is good since targets check with their if attribut whether a property is set or not.

so you do something like :

<fileset dir="foo/bar" id="myfileset"/>
<target name="fileset.check">
    <pathconvert refid="myfileset" property="fileset.notempty"
setonempty="false"/>
</target>
<target name="main" depends="fileset.check" if="fileset.nonempty">
    <!-- your main work goes here -->
</target>
like image 100
tonio Avatar answered Oct 19 '22 09:10

tonio