NAnt:如何从其他目录中替换一组文件

时间:2011-05-10 11:41:21

标签: .net nant

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

这将是此任务的伪代码:

  foreach (string file in /foo/var)
  {
    srcFile = "/other/dir/" + GetFileName(file);
    Copy(srcFile, file);
  }

我只需要在/foo/var/other/dir中都存在替换文件。 /foo/var中也存在/other/dir中不存在的文件,反之亦然。

使用NAnt的最佳方法是什么?

2 个答案:

答案 0 :(得分:2)

这应该可以胜任。它只会查看根目标目录中的文件并忽略子文件夹。如果您希望将文件包含在子文件夹中,则需要更多工作

<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

测试了它

答案 1 :(得分:1)

我认为你几乎可以使用你的伪代码示例。请参阅the foreach taskthe copy task。并the path manipulation functions

相关问题