在目标目录中检查重复文件的 Ant 任务 [英] Ant task to chek duplicate file in destination directory

查看:29
本文介绍了在目标目录中检查重复文件的 Ant 任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将文件列表从源目录复制到目标目录,在复制之前我需要检查重复的文件名..

I am copying list of files from source directory to destination directory, i need to check duplicate file name before copying..

谢谢..

推荐答案

只是想知道,这是否足够?

Just wondering, would this be sufficient?

<copy todir="../new/dir" overwrite="false" verbose="true">
    <fileset dir="src_dir"/>
</copy>

如手册所述(http://ant.apache.org/manual/Tasks/copy.html ):

verbose - 记录正在复制的文件.

verbose - Log the files that are being copied.

覆盖 - 即使目标文件更新也覆盖现有文件.

overwrite - Overwrite existing files even if the destination files are newer.

因为这将是一个省力的解决方案.否则,我认为您需要创建自己的蚂蚁任务.

As this would be a low effort solution. Otherwise, I think you need to create your own ant task.

更新:

好的,所以我检查了 ant copy 任务的来源,我相信您可以通过在您的子类中扩展它来完成所需的任务(新的 ant 任务).我假设您正在运行多个文件复制而不是一个.

OK, so I checked the sources of ant copy task, and I believe you can do the required by extending it in your subclass (new ant task). As I assume you're running multiple files copying instead of just one.

所以你需要:

  • 好吧,这不是必须的,但我相信它会对您有所帮助:下载 Ant 的源代码(您正在使用的版本),假设您使用的是最新版本:http://ant.apache.org/srcdownload.cgi
  • 创建您自己的蚂蚁任务.有关如何执行和使用它的文档,请参阅:http://ant.apache.org/manual/develop.html(查看示例部分)
  • 确保您的类扩展类:org.apache.tools.ant.taskdefs.Copy
  • 覆盖超类方法 doFileOperations,正如 javadoc 所说:

    • well this is not a must, but I believe it would help you: download source code of Ant (version you're using), let's assume you use the latest one: http://ant.apache.org/srcdownload.cgi
    • create your own ant task. For docs how to do it and use it, see: http://ant.apache.org/manual/develop.html (check example section)
    • make sure your class extends class: org.apache.tools.ant.taskdefs.Copy
    • override superclass method doFileOperations, as it's javadoc says:

      实际上会复制文件(也可能是空目录).这是子类重写的好方法.

    • Actually does the file (and possibly empty directory) copies. This is a good method for subclasses to override.

      @Override
      protected void doFileOperations() {
          if (fileCopyMap.size() > 0) {
      
          Enumeration e = fileCopyMap.keys();
          while (e.hasMoreElements()) {
              String fromFile = (String) e.nextElement();
              String[] toFiles = (String[]) fileCopyMap.get(fromFile);
      
              for (int i = 0; i < toFiles.length; i++) {
                  String toFile = toFiles[i];
      
                  if (fromFile.equals(toFile)) {
                      log("Skipping self-copy of " + fromFile, verbosity);
                      continue;
                  }
      
                  if (new File(toFile).exists) {
                      log ("Warning: dest file already exists");
                  }
              }
          }
                  ...
      
          super.doFileOperations();
      }
      

      这篇关于在目标目录中检查重复文件的 Ant 任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆