Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 734
ktraos Avatar asked May 21 '13 06:05

ktraos


People also ask

How do I get the name of a file with no extension?

} You can call the file.getName () method that returns the name of the file as String. Then you cut the extension. String fileName = file.getName (); fileName = fileName.substring (0, fileName.lastIndexOf (".")+1); If the file has no extension, the fileName will become "".

How to remove all extensions from a filename in Python?

But if we want to remove all extensions from the filename, “ baeldung ” is the correct result “ .baeldung “: Since this filename doesn't have any extension either, the filename shouldn't be changed either. Thus, we're expecting to see “ .baeldung ” in the result

Does guava's getnamewithoutextension() method work for dotfiles without an extension?

Therefore, Guava's getNameWithoutExtension () method won't work for dotfiles without an extension. Let's write a test to prove that: When we handle a filename with multiple extensions, this method doesn't provide an option to remove all extensions from the filename: 4. Testing the Apache Commons IO Library

What is the difference between single file and dotfile with multiple extensions?

With a single extension, this is the most usual case, for example, “ baeldung.txt “ Dotfile with a single extension, for instance, “ .baeldung.conf “ Dotfile with multiple extensions, for example, “ .baeldung.conf.bak “ Next, we'll list expecting results of the examples above after removing the extension (s):


1 Answers

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>
like image 61
Rebse Avatar answered Sep 30 '22 13:09

Rebse