views:

644

answers:

3

Hi,

Given a fileset

<fileset id="myFiles" dir=".">
    <include name="**/*.file"/>
</fileset>

How do I create a sub-directory at each file in the set, named after the filename without the extension?

For example, given the files folderA/X.file and folderA/folderB/Y.file, I want to create the directories folderA/X and folderA/folderB/Y

+1  A: 

You would be using for task to iterate on your file list. But I have not come across any substring type of utility in Ant which you can use to strip the extension and create the directory. Do search for this utility, if its not there then you need to implement an Ant task to do that.

Bhushan
A: 

Sorry to answer my own question. Unless someone knows otherwise, there appears to be no way for out-of-the-box ANT to create directories (e.g. using mkdir) relative to entries in a fileset.

Ant-Contrib contains useful for loop tasks, as Bhushan suggests, which could possibly perform this sort of task.

Had some better things to be getting on with, so in the end, I just wrote a batch file called by an ANT task (apply tasks can iterate over filesets).

<apply executable="cmd" failonerror="1">
    <arg value="/c"/>
    <arg line="build\tools\makeRelDir.bat"/>
    <fileset dir=".">
        <include name="**/*.file"/>
    </fileset>
</apply>

where the batch file does this: mkdir %~dp1%~n1

(Why is it so hard to do something some simple in ANT? Am I missing something?)

Andy
+2  A: 

The ant touch task supports creating files, parent dirs and filename mapping, so can be used to achieve this:

  <target name="mkdirs">
    <touch mkdirs="true">
      <fileset dir="the_dir"/>
      <mapper type="glob" from="*.file" to="the_dir/*/.tmp" />
    </touch>
    <delete>
      <fileset dir="the_dir" includes="**/.tmp"/>
    </delete>
  </target>

This creates temporary files in target dirs (creating the dirs if the don't exist) then deletes the temporary files leaving the dirs you wanted.

sudocode