使用映射器和文件集将文件复制到不同的子目录? [英] Using mapper & fileset to copy files into a different subdirectory?

查看:31
本文介绍了使用映射器和文件集将文件复制到不同的子目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个 Ant 目标,它将目录中的文件复制到具有相同文件夹结构的目标目录,再加上一个附加的子文件夹.

I want to create an Ant target that copies files in a directory to a destination directory with the same folder structure, plus one more subfolder appended.

例如来源是:

a/b/c/foo.pdf
d/e/f/bar.pdf

我希望目的地是:

a/b/c/x/foo.pdf
d/e/f/x/bar.pdf

到目前为止,这是我的目标,但它似乎没有做任何事情:

Here is my target so far, but it doesn't appear to be doing anything:

<copy todir="${dest.dir}">
   <fileset dir="${src.dir}" casesensitive="yes">
       <include name="**${file.separator}foo.pdf" />
   </fileset>      
   <mapper type="glob"
           from="foo.pdf" to="x${file.separator}foo.pdf" />            
</copy>

我错过了什么?

推荐答案

你可以使用 regexp 映射器:

You could use a regexp mapper:

<copy todir="${dest.dir}">
    <fileset dir="${src.dir}" casesensitive="yes">
        <include name="**/*.pdf"/>
    </fileset>
    <mapper type="regexp" from="^(.*)/(.*.pdf)" to="1/x/2" />
</copy>

我使用硬编码的 file.separators 来缩短.基本上,您将输入文件(来自)的路径拆分为目录和文件名(捕获 12),然后插入 x 它们之间的额外元素(to).

I've used hard-coded file.separators to shorten. Basically, you split the path to the input file (from) into directory and filename (capture 1 and 2) and then insert the x extra element between them (to).

我不清楚您的示例 - 看起来您想要匹配bar.pdf"并将其重命名为foo.pdf",以及更改目录.如果您需要这样做,您可以考虑链接几个更简单的正则表达式映射器,而不是尝试制作一个复杂的映射器:

I'm not clear on your example - it looks like you want to match 'bar.pdf' and rename it to 'foo.pdf', as well as changing the directory. If you need to do that, you might consider chaining a couple of simpler regexp mappers, rather than trying to cook up one complex one:

<copy todir="${dest.dir}">
    <fileset dir="${src.dir}" casesensitive="yes">
        <include name="**/*.pdf"/>
    </fileset>
    <chainedmapper>
        <mapper type="regexp" from="^(.*)/(.*.pdf)" to="1/x/2" />
        <mapper type="regexp" from="^(.*)/(.*.pdf)" to="1/foo.pdf" />
    </chainedmapper>
</copy>

当使用 glob mapper,需要在from字段中指定一个通配符*:

to 和 from 都是必需的,并且定义可能包含的模式大多数*.对于每个源文件匹配 from 模式,一个目标文件名将从通过将 * 替换为模式to 与文本的模式匹配 from 模式中的 *.源文件名不匹配from 模式将被忽略.

Both to and from are required and define patterns that may contain at most one *. For each source file that matches the from pattern, a target file name will be constructed from the to pattern by substituting the * in the to pattern with the text that matches the * in the from pattern. Source file names that don't match the from pattern will be ignored.

所以这样的事情可能会起作用:

So something like this might work:

<mapper type="glob" from="*/foo.pdf" to="*/x/foo.pdf" />

这篇关于使用映射器和文件集将文件复制到不同的子目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆