【问题标题】:Ant: Rename sub-directories of the same nameAnt:重命名同名子目录
【发布时间】:2012-05-17 12:55:13
【问题描述】:

假设我有一个这样的目录结构:

  • 动物/狗/细节
  • 动物/猫/细节
  • 动物/青蛙/细节
  • 动物/马/细节

使用 ant,我想将 animals 下名为 details 的所有子目录重命名为 new。所以结果是这样的:

  • 动物/狗/新
  • 动物/猫/新
  • 动物/青蛙/新
  • 动物/马/新

我尝试过这样的事情:

    <move tofile="new">
        <path id="directories.to.rename">
            <dirset dir="animals">
                <include name="**/details"/>
            </dirset>
        </path>
    </move>

但是得到这个错误:

Cannot concatenate multiple files into a single file.

【问题讨论】:

标签: java ant rename


【解决方案1】:

使用Ant-Contribfor 任务和propertyregex 任务。

<target name="test">
  <for param="detailsDir">
    <dirset dir="animals">
      <include name="**/details"/>
    </dirset>
    <sequential>
      <propertyregex property="output.dir" input="@{detailsDir}" regexp="(.*)/details" replace="\1" />
      <move file="@{detailsDir}" toFile="${output.dir}/new" />
    </sequential>
  </for>
</target>

【讨论】:

  • 由于我的 ant-contrib 版本使用 foreach(而不是 for),所以我不得不做一些 tweeking,但是您所拥有的核心思想非常有效。谢谢!
【解决方案2】:

您可以通过mapper 进行您描述的重命名。例如:

<move todir="animals">
    <dirset dir="animals" includes="**/details" />
    <globmapper from="*/details" to="*/new"/>
</move>

(在move task docs的末尾有一个类似的例子。)

您看到的错误是因为您将移动任务 (tofile) 的单文件模式与多文件模式混合在一起。 无需将dirset 嵌套在path 中,因为move 任务接受任何基于文件的资源集合,包括dirset

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多