ANT How to delete ONLY empty directories recursively

只谈情不闲聊 提交于 2020-01-13 08:04:04

问题


Does anyone know how to recursively delete "empty" directories with ANT (empty includes directories that only contain ".svn" etc).

I know ant allows you to "includeEmptyDirs=true" but I want it to ONLY delete a directory if it is empty (and actually I'd probably need to walk up the recursive chain and delete the directory it was contained in if it is now empty).

Basically as part of our build process we copy over a set of directories that contain a bunch of other nested directories containing various XML and data, and when we move the location for that data our "copy" and checkin build process doesn't really work, and because we are checking into another source control (SVN), wiping out the directories and copying over isn't really an option either (we'd be blowing away the ".svn" folders).

Before we copy over the new builds I can "clear" out the directories by doing the following:

<delete>
  <fileset dir="${webplatformBin}" includes="**/*"/>
</delete>

This leaves every directory (with the ".svn") as an empty directory and then I copy over the new files. After they're copied I'm not sure how I can clear out the empty directories that are left (if we've completely moved where the top-level data directory is etc.).

For example if I had a /projectA/data/localization/text.xml file and I moved it to /projectB/data/localization/text.xml, I would end up with an empty folder /projectA/data/localization/ (that would only contain a .svn folder).


回答1:


Here's the best answer I've been able to come up with:

<delete includeemptydirs="true">
  <fileset dir="${dirToStartFrom}"  >
    <and>
      <size value="0"/>
      <type type="dir"/>
     </and>
  </fileset>
</delete>

I then wrapped it in a macro so I can pass the dir name in from any target:

<!-- Find and delete empty folders under dir -->
<macrodef name="deleteEmptyFolders">
    <attribute name="dir"/>
    <sequential>
        <delete includeemptydirs="true">
            <fileset dir="@{dir}"  >
                <and>
                    <size value="0"/>
                    <type type="dir"/>
                </and>
            </fileset>
        </delete>
    </sequential>
</macrodef>

Like so:

<target name="clean">
  <deleteEmptyFolders dir="build"/>
  <deleteEmptyFolders dir="common"/>
  <deleteEmptyFolders dir="lib"/>
</target>



回答2:


Here's what I cooked up:

<!-- next three targets are connected
     To remove empty folders from XXX folder.  Process is recursed 3 times.  This 
     is necessary because parent directories are not removed until all their children
     are (if they are empty), and parents are processed before children 
      My example will process structures 3 deep, if you need to go deeper
      then add members to the list  like list="1,2,3,x,x,x,x,x,x"  -->

<target name="rmmtdirs">
        <foreach list="1,2,3" target="rmmtdirs_recurse" param="num"/>
</target>

<target name="rmmtdirs_recurse">
        <foreach target="rmmtdir" param="rmdir">
            <path>
                <dirset dir="${XXX}"/>
            </path>
        </foreach>
</target>

<target name="rmmtdir">
        <echo message=" Removing: ${rmdir} "/>
        <delete includeemptydirs="true">
                <fileset dir="${rmdir}" excludes="**/*"/>
        </delete>
</target>



回答3:


If it is not sufficient to completely clear the target location (use defaultExcludes="false" to ensure the .svn folders are deleted), you could try writing a custom ant task to traverse the file system below the target, deleting empty directories as you move back up from each leaf.




回答4:


This is probably easier to do with a batch file that gets called from ant.

You can use Raymond Chen's script, but it doesn't work if there are spaces in the names.




回答5:


I was able to delete all the empty directories starting with the current working directory with the following:

<delete includeemptydirs="true">
    <fileset dir="." includes="**" excludes="**/*.*" />
</delete>


来源:https://stackoverflow.com/questions/1472803/ant-how-to-delete-only-empty-directories-recursively

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!