.net - NAnt:如何替换其他目录中的一组文件

标签 .net nant

我需要将某个目录中包含的所有文件替换为其他目录中存在的所有文件。

这将是此任务的伪代码:

  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/

相关文章:

.net - 如何在Web网格的列中编写IF条件

c# - 如果三个参数中只有一个或两个为真,则返回 True 的代码

cruisecontrol.net - 在开发人员/构建机器上部署 biztalk

nant - 如何从另一个构建文件执行 nant 构建文件?

c# - 什么是 nAnt,它如何对我作为 C# 开发人员有用?

c# - 枚举上最常见的 C# 按位运算

.net - 按特定顺序对 SelectList 进行排序

svn - 使用 NAnt 和 Ivy 构建 VB6 项目

ant - 相当于 Ant Replace 任务的 Nant

c# - C#中运行对象表访问其他excel实例的Excel.Application对象