gpt4 book ai didi

groovy - 在 Groovy 的 file.eachFileMatch() 中访问模式变量的 ant 属性值

转载 作者:行者123 更新时间:2023-12-05 01:12:44 25 4
gpt4 key购买 nike

当我在 macrodef 中的 groovy 任务中使用 ant macrodef 属性时,我无法运行移动文件的代码。

<macrodef name="dirmove">
<attribute name="todir" />
<attribute name="fromdir" />
<attribute name="includes" default="*" />
<sequential>
<var name="todir" value="@{todir}" />
<var name="fromdir" value="@{fromdir}" />
<var name="includes" value="@{includes}" />
<groovy>
File dir1 = new File(properties.'fromdir');
File dir2 = new File(properties.'todir');
def pattern = properties.get('includes')
println pattern;
dir1.eachFileMatch ~/pattern/, {
f->
boolean fileMoved = f.renameTo(new File(dir2, f.getName()));
//assert f.name == '1.txt' //**because File object is immutable, so I am just checking for the existing of previous file name. It is still there.
println fileMoved;
}
</groovy>
</sequential>
</macrodef>

此代码正确打印了来自属性值的模式值。但是 eachFileMatch 函数没有选择规范

最佳答案

            dir1.eachFileMatch  ~/pattern/, { 

应该是

            dir1.eachFileMatch  ~/${pattern}/, { 

由于模式是一个字符串变量,因此您需要将其添加到正则表达式模式中。

以前,您只是搜索所有名为 pattern 的文件

关于groovy - 在 Groovy 的 file.eachFileMatch() 中访问模式变量的 ant 属性值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9062517/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com