Menu

<For>Get FileName,not File Full Path, How?

Help
jinyu
2005-05-23
2013-04-24
  • jinyu

    jinyu - 2005-05-23

    I use <for> like below:
        <for param="file">
          <path>
            <fileset dir="${Dir_Work}">
              <include name="**/*.java"/>
            </fileset>
          </path>
          <sequential>
            <exec
    executable="compile.exe">
                -o @{file}"/>
            </exec>
          </sequential>
    </for>

    here the @{file} I only need the file name(abc.java), but not the full path with fileName(d:/xyz/abc.java), Can you help me?

    Thanks!

     
    • Curt Arnold

      Curt Arnold - 2005-05-23

      Use Ant's <basename> task.  Something like:

      <sequential>
          <basename property="file.filename" file="${file}"/>
          <exec executable="compile.exe">
                <arg value="${file.filename}"/>
          </exec>
      </sequential>

      Hope that is close enough to get you going.
       

       
    • jinyu

      jinyu - 2005-05-23
      I think it's what I want.
      but after testing, I find the ${file.filename} just get the first file name, it can't get others. I don't know why .
      (
       
    • jinyu

      jinyu - 2005-05-23

      It should be:
      a0.java, b1.java, c2.java
      I print the filename, it's
      [echo] file.filename = a0.java
      [echo] file.filename = a0.java
      [echo] file.filename = a0.java

      Is it a bug for ant or ant-contrib?

       
      • Curt Arnold

        Curt Arnold - 2005-05-23

        Sorry, Ant properties are immutable once set.  You may need use antcall within the for loop and invoke basename within the called target.  Not a bug on anybody, other than my example was a little hasty.

         
    • jinyu

      jinyu - 2005-05-23

      I don't understand.

      I tried below:
      <for param="file">
      <path>
      <fileset dir="${Dir_Work}">
      <include name="**/*.java"/>
      </fileset>
      </path>
      <sequential>
      <antcall target="callCompile"/>
      </sequential>
      </for>

      then I define a target named callCompile:
      <target name="callCompile">
      <basename property="file.filename" file="@{file}"/>                           
      <exec 
      executable="compile.exe">
      -o ${file.filename}"/>
      </exec>
      </target>

      It couldn't get @{file}, Can you show me some code to let me know your opinion?
      Thanks very much!

       
      • Curt Arnold

        Curt Arnold - 2005-05-23

        Sorry under deadline on something else right now and unable to spend any time on this. 

        You may need to pass @file as parameter to antcall target:

        <for param="file"> 
        <path> 
        <fileset dir="${Dir_Work}"> 
        <include name="**/*.java"/> 
        </fileset> 
        </path> 
        <sequential> 
        <antcall target="callCompile">
           <param name="file" value="@{file}"/>
        </antcall>
        </sequential> 
        </for> 

        then I define a target named callCompile:
        <target name="callCompile">
        <basename property="file.filename" file="${file}"/> 
        <exec 
        executable="compile.exe"> 
        -o ${file.filename}"/> 
        </exec> 
        </target>

         
    • jinyu

      jinyu - 2005-05-23

      Get it! :D

      I'm really a rookie!!

      Thank you very much!!

       

Log in to post a comment.