質問

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

役に立ちましたか?

解決

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>

他のヒント

The problem is that you are on a PC, and when you use ${basedir}, it's giving you the ${basedir} with backward Windows slashes for directory separators. The <fileset/> is doing the same.

I ran into this very same situation, and I had to first do a regexreplace on my path, then do my substitution. Here's the code directly from my project:

            <var name="dest.name"               unset="true"/>
            <var name="file.lastmodified"       unset="true"/>
            <var name="file.type"               unset="true"/>

            <file.mdate file="@{file}"
            property="file.lastmodified"/>

            <propertyregex property="file"
            input="@{file}"
            regexp="\\"
            replace="/"
            override="true"
            defaultvalue="@{file}"/>

            <propertyregex property="dest.name"
            input="${file}"
            regexp="^${fileset}/"
            replace=""
            override="true"/>

Note that I now have to use <var/> to unset the properties, so I can use them over and over in my <for> loop:

The first regexreplace will change

C:\projects\Dev\Main\Sample Game\js\simple\welcome\ss.js

to

C:/projects/Dev/Main/Sample Game/js/simple/welcome/ss.js

From there, you can use the basedir task to remove the script. Then, you just need to for the /js/ directory.

 <regexreplace property="currentdirectory"
    file="@{file}"
    regex=".*/js/(.*)"
    select="\1"
    override="true"/>
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top