【问题标题】:Copy specific files from a directory and subdirectories into a target folder in mac将目录和子目录中的特定文件复制到mac中的目标文件夹中
【发布时间】:2018-09-10 03:52:34
【问题描述】:

我有一个目录结构,其中我有一些特定的文件(比如 mp3 文件)直接组织或在子目录中(最多 n 级)。 例如:

  • 音乐文件夹
    • a.mp3
    • 文件夹2
    • 文件夹 3
    • b.mp3
    • c.mp3
    • 文件夹 4
    • d.mp3
    • 文件夹5
    • 文件夹6
      • 文件夹7
      • 文件夹8
        • e.mp3

现在,我需要将所有文件夹和子文件夹中的所有文件(.mp3 文件)复制到另一个目标文件夹中。所以我的目标文件夹是这样的:

  • 目标文件夹
    • a.mp3
    • b.mp3
    • c.mp3
    • d.mp3
    • e.mp3

我尝试了以下问题的答案: Recursive copy of specific files in Unix/Linux?

Copy all files with a certain extension from all subdirectories,但复制了相同的目录(和子目录)结构。

有什么帮助或建议吗?

【问题讨论】:

    标签: java command-line copy directory-structure subdirectory


    【解决方案1】:
    cd "Music Folder"
    find . -name "*.mp3" -exec cp {} /path/to/targetFolder \;
    

    【讨论】:

      【解决方案2】:

      使用java代码,我实现了如下:

      Path start = Paths.get("/Users/karan.verma/Music/iTunes/iTunes Media/Music");
              int maxDepth = 15;
              try(Stream<Path> stream = Files.find(start, 
                          maxDepth, 
                          (path, attr) -> String.valueOf(path).endsWith(".mp3"))){
      
                  List<Path> fileName = stream
                          .sorted()
                          .filter(path -> String.valueOf(path).endsWith(".mp3"))
                          .collect(Collectors.toList());
      
                  for(Path p : fileName) {
                      Path path = Paths.get("/Users/karan.verma/Desktop/TestCopy/"+p.getFileName());
                      Files.copy(p, path,StandardCopyOption.REPLACE_EXISTING);
                  }
              }catch(Exception e){
                  e.printStackTrace();
              }
      

      【讨论】:

        猜你喜欢
        • 2019-11-01
        • 2014-07-25
        • 1970-01-01
        • 2019-12-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多