ant - Need to get only file name without extension and entire path
Asked Answered
K

1

5

I have couple of .xml files in a folder.I want to loop on each .xml file.It is getting fine. I want to take only .xml file name without entire path. How can i achieve that.?

I am using the below piece of code to get the file name.

<target name="createApplicationDAA">
  <for param="program">
    <path>
      <fileset dir="${soaProjectName}/Composites" includes="**/*.xml"/>
    </path>
    <sequential>
    <propertyregex override="yes" property="file"  input="@{program}" regexp=".*/([^\.]*)\.xml" replace="\1"/>
        <echo>@{program}</echo>
    </sequential>
  </for>
</target>

the folder name is C:/abc/bcd/cde first.xml,second.xml,third.xml,fourth.xml is the .xml files in cde folder. when i execute the above code it is getting entire path like C:/abc/bcd/cde/first.xml ..etc I want to get only first for first.xml and second for second.xml. please help me out to achieve only file name.

Khania answered 21/5, 2013 at 6:19 Comment(1)
There is a basename task in ANT that you could use instead of regexp: ant.apache.org/manual/Tasks/basename.htmlSchilit
F
9

EDIT after further investigation (see comments)

No need for regex when using basename task. Properties once set are immutable in vanilla ant, so when using basename task within for loop, the property FileName holds the value of the first file.
Therefore antcontrib var task with unset="true" has to be used :

 <for param="program">
  <path>
   <fileset dir="C:\whatever" includes="**/*.xml"/>
  </path>
  <sequential>
   <var name="FileName" unset="true"/>
   <basename property="FileName" file="@{program}" suffix=".xml"/>
   <echo>${FileName}</echo>
  </sequential>
 </for>
  1. the regex doesn't work for me on my windowsbox
  2. you're echoing the original filename when using <echo>@{program}</echo>
    use <echo>${file}</echo> instead

means something like :

<for param="program">
 <path>
  <fileset dir="C:\whatever" includes="**/*.xml"/>
 </path>
 <sequential>
 <propertyregex override="yes" property="file" input="@{program}" regexp=".:\\.+\\(\w+).+" replace="\1"/>
 <echo>${file}</echo>
 </sequential>
</for>
Fishgig answered 21/5, 2013 at 7:54 Comment(5)
Hi Rebse,Thanks for your support.It worked great and i got the same with base name task.Thank you once againKhania
basename task still has the file extension .xml, you wrote you want first and second instead of first.xml and second.xml !?Fishgig
we can use the attribute suffix for basename task to avoid the extension.so i used the same as below. <basename property="FileName" file="${program}" suffix=".xml"/>Khania
Beware - properties once set are immutable in vanilla ant, so when using basename task within for loop, the property FileName holds the value of the first file, whereas the propertyregex task of antcontrib allows to override existing propertyvalues via override attribute set to 'yes'Fishgig
Forgot about the var task of antcontrib - it's been a while => you may use the basename task within for loop but you have to put this line before => <var name="FileName" unset="true"/>, also note the syntax error file="${program}" in your comment => within for loop use @{param} not ${param}Fishgig

© 2022 - 2024 — McMap. All rights reserved.