Search code examples
javaregexfor-loopantant-contrib

Ant Propertyregex part of file path


I am looping list of files inside base directory using for of Ant-Contrib library. I want to get the part of file path after base directory but without filename.

For example my base Directory is : C:\projects\Dev\Main\Sample Game\js I have lot of js scripts and inside this folder and subfolders name like

C:\projects\Dev\Main\Sample Game\js\simple\welcome\ss.js
C:\projects\Dev\Main\Sample Game\js\hard\welcome\cc.js
C:\projects\Dev\Main\Sample Game\js\easy\welcome\ee.js

I want to get only \simple\welcome\. I am using the below code.

http://pastebin.com/TpqXBb27

<for param="filename">
    <path id="project.fileset">
        <fileset dir="${basedir}/js" includes="/*">
            <include name="**/*.js" />
        </fileset>
    </path>
    <sequential>
        <basename property="file.@{filename}" file="@{filename}"/>
        <propertyregex property="currentdirectory" 
            input="@{filename}"
            regexp="${basedir}/js//([^//]*)//${file.@{filename}}" 
            select="\1"
            casesensitive="false"
            override="true" />
        <echo message="FullPath:@{filename}" />
        <echo message="Directory:${currentdirectory}" />
    </sequential>
</for>

I don't know what regex I should provide there... I tried with regex ${basedir}/js//([^//]*)//${file.@{filename}}, but getting the below error

java.util.regex.PatternSyntaxException: Illegal/unsupported escape sequence near index 3
C:\projects\Dev\Main\Sample Game\js\simple\welcome\ss.js

Please give some suggestion for this regex


Solution

  • This sounds like more of a job for pathconvert rather than propertyregex

    <for param="filename">
        <path id="project.fileset">
            <fileset dir="${basedir}/js" includes="/*">
                <include name="**/*.js" />
            </fileset>
        </path>
        <sequential>
            <basename property="file.@{filename}" file="@{filename}"/>
            <!-- dirname strips off the trailing file name, leaving the full dir -->
            <dirname property="dir.@{filename}" file="@{filename}"/>
            <pathconvert property="currentdirectory.@{filename}">
                <file file="${dir.@{filename}}" />
                <!-- pathconvert strips off the leading ${basedir}/js -->
                <map from="${basedir}/js" to="" />
            </pathconvert>
            <echo message="FullPath:@{filename}" />
            <echo message="Directory:${currentdirectory.@{filename}}" />
        </sequential>
    </for>