【发布时间】:2012-09-12 18:44:56
【问题描述】:
我有几个程序的输出需要与“预期”输出文件夹进行比较。但我的问题是输出文件总是有不同的文件名。
如何将一个文件夹中的所有文件重命名为另一个文件夹中的相同文件名。这里的例子 - 预期的文件结构(当然可以改变):
expected/folder1/file1.txt
expected/folder1/file2.txt
expected/folder2/file1.txt
expected/folder3/file1.txt
我的输出看起来像这样(文件数和位置总是相等的):
result/folder1/fileOtherName1.txt
result/folder1/fileOtherName2.txt
result/folder2/fileOtherName1.txt
result/folder3/fileOtherName1.txt
我尝试使用 ANT(因为我知道),但被卡住了,因为我无法按索引选择文件(按字母顺序排序)。
这是我在 ANT 中的伪代码(但不知道如何继续):
<target name="foo">
<foreach>
<fileset dir="result" casesensitive="yes">
<include name="**/*.txt"/>
</fileset>
<antcall target="rename">
</antcall>
</foreach>
</target>
<target name="rename">
<!-- how can I access another fileset and take the correct file? -->
<!-- Here I got stuck -->
<echo message="foreach.file is ${foreach.file}" />
<echo message="foreach.dir is ${foreach.dir}" />
<echo message="foreach.name.ext is ${foreach.name.ext}" />
<echo message="foreach.name is ${foreach.name}" />
</target>
感谢您的帮助,它不能仅在 ANT 中 - BASH 脚本或类似脚本也可以完成这项工作。
【问题讨论】:
-
你怎么知道哪个结果文件名应该匹配哪个预期的文件名?你只是删除一个固定的部分吗?我还缺少其他东西吗?
-
这取决于每个文件夹中文件的字母顺序。所以“fileOtherName1”应该重命名为file1等等。结果中的第一个文件重命名为预期中的第一个文件名。
标签: file bash ant rename batch-processing