我需要将某个目录中包含的所有文件替换为其他目录中存在的所有文件。
这将是此任务的伪代码:
foreach (string file in /foo/var)
{
srcFile = "/other/dir/" + GetFileName(file);
Copy(srcFile, file);
}
仅当 /foo/var
和 /other/dir
中都存在该文件时,我才需要替换该文件。此外,还有一些文件存在于 /foo/var
中,但不存在于 /other/dir
中,反之亦然。
使用 NAnt 实现此目的的最佳方法是什么?
最佳答案
这应该可以完成工作。它只会查看根目标目录中的文件并忽略子文件夹。如果您希望它包含子文件夹中的文件,则需要做更多的工作
<project name="Sync" default="sync" xmlns="http://nant.sf.net/release/0.85/nant.xsd">
<property name="source.path" value="D:\test\source\" />
<property name="target.path" value="D:\test\target\" />
<target name="sync" description="Copies only the matching files to a folder">
<foreach item="File" property="targetFile">
<in>
<items basedir="${target.path}"> <!-- include all files from the target path -->
<include name="*" /> <!-- this will not include subfolders -->
</items>
</in>
<do>
<if test="${file::exists(source.path + path::get-file-name(targetFile))}">
<copy
file="${source.path + path::get-file-name(targetFile)}"
tofile="${targetFile}"
overwrite="true"
/>
</if>
</do>
</foreach>
</target>
</project>
我用 Nant 0.86 测试过
关于.net - NAnt:如何替换其他目录中的一组文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5949455/