Discussion:
How to delete all files in a dirset
Al Le
2016-10-20 10:08:15 UTC
Permalink
Hello,

I have a dirset defined in my script. How can I delete all files contained in that dirset?

Actually, my goal is to delete the dirs from the dirset, including all the files contained in them. But since the delete task does not delete non empty dirs, I'd first delete all the files and, in a second call to 'delete', I'd delete the dirs.

But how can I delete the files?

I.e.

<!-- Define the set of dirs to delete so that id can be referenced by the id -->
<dirset dir="${srcDir}" id="dirs.to.delete">
<include name="*_to-delete"/>
</dirset>

<!-- Delete all the files contained in 'dirs.to.delete' -->
<delete>
<!-- What should I specify here? -->
<delete/>

Thanks!
AL

---------------------------------------------------------------------
To unsubscribe, e-mail: user-***@ant.apache.org
For additional commands, e-mail: user-***@ant.apache.org
Jan Matèrne (jhm)
2016-10-20 10:56:38 UTC
Permalink
Could you use a <fileset>?

Jan

> -----Ursprüngliche Nachricht-----
> Von: Al Le [mailto:***@gmx.de]
> Gesendet: Donnerstag, 20. Oktober 2016 12:08
> An: ***@ant.apache.org
> Betreff: How to delete all files in a dirset
>
> Hello,
>
> I have a dirset defined in my script. How can I delete all files
> contained in that dirset?
>
> Actually, my goal is to delete the dirs from the dirset, including all
> the files contained in them. But since the delete task does not delete
> non empty dirs, I'd first delete all the files and, in a second call to
> 'delete', I'd delete the dirs.
>
> But how can I delete the files?
>
> I.e.
>
> <!-- Define the set of dirs to delete so that id can be referenced by
> the id --> <dirset dir="${srcDir}" id="dirs.to.delete">
> <include name="*_to-delete"/>
> </dirset>
>
> <!-- Delete all the files contained in 'dirs.to.delete' --> <delete>
> <!-- What should I specify here? --> <delete/>
>
> Thanks!
> AL
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: user-***@ant.apache.org For additional
> commands, e-mail: user-***@ant.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: user-***@ant.apache.org
For additional commands, e-mail: user-***@ant.apache.org
Al Le
2016-10-20 11:56:28 UTC
Permalink
> Could you use a <fileset>?

I could use a fileset with "type=dir" filter, yes. But then I'd still have a problem of how to delete all files in these dirs.

For now, I just use "apply" and execute "cmd /c rmdir /s /q <file>" on each dir (I'm on windows).

AL

---------------------------------------------------------------------
To unsubscribe, e-mail: user-***@ant.apache.org
For additional commands, e-mail: user-***@ant.apache.org
Jan Matèrne (jhm)
2016-10-20 12:59:10 UTC
Permalink
If I understand your requirements right, you want to delete recursively all directories which contain a marker file.

So maybe you could use this (have a deeper look at the <delete> task).


Jan



<project xmlns:au="antlib:org.apache.ant.antunit" xmlns:if="ant:if" default="antunit">

<property name="test.dir" value="test" />

<!-- from Ant's src/tests/antunit/antunit-base.xml -->
<target name="antunit">
<antunit xmlns="antlib:org.apache.ant.antunit">
<plainlistener />
<file file="${ant.file}" xmlns="antlib:org.apache.tools.ant" />
</antunit>
</target>

<macrodef name="create">
<attribute name="dir" />
<attribute name="delete" default="false"/>
<sequential>
<mkdir dir="@{dir}" />
<touch file="@{dir}/file.txt" />
<touch file="@{dir}/-to-delete-" if:true="@{delete}"/>
</sequential>
</macrodef>

<target name="setUp">
<create dir="${test.dir}/dir1/dir11/dir111" />
<create dir="${test.dir}/dir1/dir11/dir112" delete="true"/>
<create dir="${test.dir}/dir1/dir12/dir121" />
<create dir="${test.dir}/dir1/dir12/dir122" />
<create dir="${test.dir}/dir2" delete="true"/>
<create dir="${test.dir}/dir2/dir21/dir211" />
<create dir="${test.dir}/dir2/dir21/dir212" />
<create dir="${test.dir}/dir2/dir22/dir221" />
</target>

<target name="tearDown">
<delete dir="${test.dir}" />
</target>

<target name="testDeleteByMarkerFile">
<!-- check prepared environment -->
<au:assertFileExists file="${test.dir}/dir1/dir11/dir111/file.txt"/>
<au:assertFileExists file="${test.dir}/dir1/dir11/dir112/file.txt"/>
<au:assertFileExists file="${test.dir}/dir2/file.txt"/>

<!-- delete -->
<delete includeemptydirs="true">
<fileset dir="${test.dir}">
<scriptselector language="javascript">
markerfile = new java.io.File(file.getParent(), "-to-delete-");
self.setSelected(markerfile.exists());
</scriptselector>
</fileset>
</delete>

<!-- check assertions -->
<au:assertFileExists file="${test.dir}/dir1/dir11/dir111/file.txt"/>
<au:assertFileDoesntExist file="${test.dir}/dir1/dir11/dir112/file.txt"/>
<au:assertFileDoesntExist file="${test.dir}/dir2/file.txt"/>
</target>

</project>



---------------------------------------------------------------------
To unsubscribe, e-mail: user-***@ant.apache.org
For additional commands, e-mail: user-***@ant.apache.org
Al Le
2016-10-20 15:07:34 UTC
Permalink
Hua, Jan, thank you for the great example!

My requirement is not quite "delete recursively all directories which contain a marker file". But very similar.

It is: Delete recursively all directories which only contain files with some name patterns but no other files. If there are other files in the dir, it should not be deleted.

I managed to create a dirset of such dirs using a scriptselector. And was wondering how to delete them. 'apply' might be a bit platform dependent, but it's simple and does the job.

Thank you again for taking time!

AL

---------------------------------------------------------------------
To unsubscribe, e-mail: user-***@ant.apache.org
For additional commands, e-mail: user-***@ant.apache.org
Loading...