Delete a folder using starts with in ANT - ant

I am comparing 2 folders A and B, and wanted to delete folders and jars present in B which are not present in folder A.
I have written the logic to get needed files to delete, but i do not wanted to delete the directory and jars starting with "com.ibm".
For that I have written delete task as below:
<delete>
<dirset dir="D://mypath/plugins<Filename to delete> excludes="**/com.ibm.*/**" />
</delete>
I have tried the excludes with the scenarios like:
excludes="**/com.ibm.*/**"
excludes="**/com.ibm.*"
excludes="com.ibm.*"
excludes="com.ibm.*/**"
But nothing works for me (It is not deleting any folders/files). Any help would be highly appreciated. Thanks !

you should use fileset instead and specify includeemptydirs="true" of delete.
<project default="init" name="My Project">
<target name="init">
<delete verbose="true" includeemptydirs="true">
<fileset dir="/home/guest/Desktop/plugins" defaultexcludes="no">
<exclude name="com.ibm.*"/>
<exclude name="com.ibm.*/**"/>
</fileset>
</delete>
</target>
</project>

Related

Ant to delete subdirectories and files in subdirectories but not any file from the directory itself

My requirement is to delete all subdirectories from a specified directory, but NOT delete files in the specified directory.
I have fussed around with fileset and dirset and I could not get a single collection to do the job. What works is this:
<delete includeemptydirs="true" verbose="true" >
<fileset dir="release/reports" >
<exclude name="*.*" />
</fileset>
<dirset dir="release/reports" includes="**/*" />
</delete>
Isn't there a way to do this with one collection (either fileset or dirset)?
It should work like that :
<delete includeEmptyDirs="true">
<fileset dir="C:/yourdir" includes="**/*" excludes="*.*"/>
</delete>

deleting Ant dirset using regex

I am trying to delete a set of directories using a regex expression, but not having much luck at all, can anyone take a look at the target please:
<delete failonerror="true" includeemptydirs="true">
<dirset dir="C:\Users\martin\Desktop\testing" includes="*">
<containsregexp expression="[0-9]*-0" />
</dirset>
</delete>
It just won't delete the files, I want it to match the folder [0-9]*-0 and if it matches it, to delete it
The delete task will delete an individual directory but not ones from a dirset.
I have the following directory structure:
C:\dev\debug_investigations\purge_deployment\test\012345-0
C:\dev\debug_investigations\purge_deployment\test\012345-1
C:\dev\debug_investigations\purge_deployment\test\987565-0
C:\dev\debug_investigations\purge_deployment\test\012345-0\sometext.txt
C:\dev\debug_investigations\purge_deployment\test\012345-0\subdir1
C:\dev\debug_investigations\purge_deployment\test\012345-0\subdir2
C:\dev\debug_investigations\purge_deployment\test\012345-0\subdir1\sometext.txt
C:\dev\debug_investigations\purge_deployment\test\012345-0\subdir2\sometext.txt
C:\dev\debug_investigations\purge_deployment\test\012345-1\sometext.txt
C:\dev\debug_investigations\purge_deployment\test\012345-1\subdir1
C:\dev\debug_investigations\purge_deployment\test\012345-1\subdir2
C:\dev\debug_investigations\purge_deployment\test\012345-1\subdir1\sometext.txt
C:\dev\debug_investigations\purge_deployment\test\012345-1\subdir2\sometext.txt
C:\dev\debug_investigations\purge_deployment\test\987565-0\sometext.txt
C:\dev\debug_investigations\purge_deployment\test\987565-0\subdir1
C:\dev\debug_investigations\purge_deployment\test\987565-0\subdir2
C:\dev\debug_investigations\purge_deployment\test\987565-0\subdir1\sometext.txt
C:\dev\debug_investigations\purge_deployment\test\987565-0\subdir2\sometext.txt
I created this ant target (ant 1.8.2)
<target name="regexptest">
<fileset dir="C:\dev\debug_investigations\purge_deployment\test\" includes="**/**" id="regexp.todelete">
<filename regex="[0-9]*-0" />
</fileset>
<!-- Which files are selected? -->
<pathconvert pathsep="${line.separator}" property="prop.regexp.test" refid="regexp.todelete" />
<echo message="Will delete:" />
<echo message="${prop.regexp.test}" />
<delete failonerror="true" includeemptydirs="true" verbose="true">
<fileset refid="regexp.todelete" />
</delete>
</target>
The directory structure after the task had run:
C:\dev\debug_investigations\purge_deployment\test\012345-1
C:\dev\debug_investigations\purge_deployment\test\012345-1\sometext.txt
C:\dev\debug_investigations\purge_deployment\test\012345-1\subdir1
C:\dev\debug_investigations\purge_deployment\test\012345-1\subdir2
C:\dev\debug_investigations\purge_deployment\test\012345-1\subdir1\sometext.txt
C:\dev\debug_investigations\purge_deployment\test\012345-1\subdir2\sometext.txt
It's more verbose than it needs to be for production, but I hope this helps future regex directory deleters!

ant clean issue with special character

If I run ant clean for
<target name="clean">
<delete file="Project.jar"/>
</target>
It deletes the jar file
if I use
<target name="clean">
<delete file="*.jar"/>
</target>
it doesn't delete the any .jar that is in the directory.
Can someone please let me know what am I missing?
Thank you
<delete>
<fileset dir="${basedir}/somedirectory" includes="*.jar"/>
</delete>
This will delete all .jar files in some directory. We can't see the special character you have anywhere in the code you provided though, so I am not sure if this is what you need...?

Using Ant to delete a file based on existence of another file

I need to write an ant task to selectively delete files.
In a directory, I have the following jars:
acme.jar
acme-201105251330.jar
I want to delete acme.jar because acme-*.jar exists.
Here's what I've tried:
<target name="-check-use-file">
<available property="file.exists">
<filepath> <fileset dir=".">
<include name="./test-*.jar"/> </fileset>
</filepath>
</available>
</target>
<target name="use-file" depends="-check-use-file" if="file.exists">
<!-- do something requiring that file... -->
</target>
Thanks
Have a look at If/Unless Attributes, examples given there seem to be exactly what you are looking for.

ANT How to delete ONLY empty directories recursively

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).
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>
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>
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>
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.
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.

Resources