Archive

Posts Tagged ‘copy’

Ant copy and move directories recursively (without ant-contrib)

July 6th, 2009 Eric No comments

As a corollary to the recent post I made about trying to recursively moving or copying directories with Ant, this post shows you how to do so without the use of the third-party ant-contrib library. The trick is understanding Ant’s wildcard syntax used in the type, which can be rather confusing.

Previously, I was trying to move a directory and all of its contents recursively to another directory using the following target

 
<target name="move.dirs">
 
    <move todir="backups">
        <fileset dir="." includes="*incremental*"/>
    </move>
 
</target>

The result was a non-recursive copy of the first level of directories with ‘incremental’ in the directory name. The only way I could accomplish a deep copy or move at the time was to use ant-contrib’s task to iterate over all files recursively. As I discovered after playing with my original script (above) a bit more, this is unnecessary.

By modifying the wildcard syntax in my original target I am able get what I want.

 
<target name="move.dir">
 
        <move todir="backups">
            <fileset dir="." includes="**/*incremental*/"/>
        </move>
 
</target>

So, the additional ‘**/’ at the beginning and a trailing ‘/’ at the end of the includes attribute value is all that is necessary. All directories (and their contents) with ‘incremental’ in the name will be moved to a directory called ‘backups/’. The same will work for the task. I won’t go into the details about how this syntax works but you can read about it here.

Related Post: Move or copy a set of directories using Ant

Categories: Code Tags: , , , ,