【问题标题】:NAnt: How to replace a set of files from other directoryNAnt:如何替换其他目录中的一组文件
【发布时间】:2011-05-10 11:41:20
【问题描述】:

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

这将是此任务的伪代码:

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

只有在/foo/var/other/dir 中都存在时,我才需要替换该文件。还有一些文件存在于/foo/var 而在/other/dir 中不存在,反之亦然。

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

【问题讨论】:

    标签: .net nant


    【解决方案1】:

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

    <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 对其进行了测试

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 2015-10-07
        • 1970-01-01
        • 2013-04-03
        • 1970-01-01
        • 2011-05-02
        • 1970-01-01
        • 2016-09-19
        • 2023-04-05
        • 1970-01-01
        相关资源
        最近更新 更多