Take the 2-minute tour ×

I am unable to run the code for moving file, when i use the ant macrodef attribute in groovy task inside the macrodef.

<macrodef name="dirmove">
            <attribute name="todir" />
            <attribute name="fromdir" />
            <attribute name="includes" default="*" />
                <var name="todir" value="@{todir}" />
                <var name="fromdir" value="@{fromdir}" />
                <var name="includes" value="@{includes}" />
                File dir1 = new File(properties.'fromdir');
                File dir2 = new File(properties.'todir');  
                def pattern = properties.get('includes')
                println pattern;
                dir1.eachFileMatch  ~/pattern/, { 
                    boolean fileMoved = f.renameTo(new File(dir2, f.getName()));
                    //assert f.name == '1.txt' //**because File object is immutable, so I am just checking for the existing of previous file name. It is still there.
                    println fileMoved;

This code correctly prints the value of pattern, which is coming from attribute value. But the eachFileMatch function doesn't picks up the spec

share|improve this question
add comment

1 Answer

up vote 0 down vote accepted
            dir1.eachFileMatch  ~/pattern/, { 

should be

            dir1.eachFileMatch  ~/${pattern}/, { 

As pattern is a String variable, so you need to add it in to the regex pattern as such.

Previously, you were just searching for all files called pattern

share|improve this answer
thanks @tim-yates..this solved the issue. –  user1173339 Jan 31 '12 at 5:19
@user1173339 Good news! :-) Can you mark this answer as accepted? –  tim_yates Jan 31 '12 at 9:53
add comment

Your Answer


