Quantcast

How to delete all files in a dirset

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

How to delete all files in a dirset

Al Le
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: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

AW: How to delete all files in a dirset

Jan Matèrne (jhm)
Could you use a <fileset>?

Jan

> -----Ursprüngliche Nachricht-----
> Von: Al Le [mailto:[hidden email]]
> Gesendet: Donnerstag, 20. Oktober 2016 12:08
> An: [hidden email]
> 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: [hidden email] For additional
> commands, e-mail: [hidden email]



---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Aw: AW: How to delete all files in a dirset

Al Le
> 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: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

AW: AW: How to delete all files in a dirset

Jan Matèrne (jhm)
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: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

AW: How to delete all files in a dirset

Al Le
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: [hidden email]
For additional commands, e-mail: [hidden email]

Loading...