将特定文件从目录和子目录复制到Mac中的目标文件夹 [英] Copy specific files from a directory and subdirectories into a target folder in mac

查看:234
本文介绍了将特定文件从目录和子目录复制到Mac中的目标文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a directory structure in which i have some specific files (say mp3 files) organised directly or in subdirectories(upto n levels). For example:


  • 音乐文件夹


    • a .mp3

    • folder2

    • folder3

    • b.mp3

    • c.mp3

    • 文件夹4

    • d.mp3

    • folder5

    • 文件夹6


      • 文件夹7

      • 文件夹8


        • e.mp3

        • Music Folder
          • a.mp3
          • folder2
          • folder3
          • b.mp3
          • c.mp3
          • folder4
          • d.mp3
          • folder5
          • folder6
            • folder7
            • folder8
              • e.mp3

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

              Now, what i require is to copy all files (.mp3 file) from all the folders and subfolders into another target folder. So my target folder would be like this:


              • targetFolder


                • a.mp3

                • b.mp3

                • c.mp3

                • d.mp3

                • e.mp3

                • targetFolder
                  • a.mp3
                  • b.mp3
                  • c.mp3
                  • d.mp3
                  • e.mp3

                  我尝试了以下方法问题:
                  在Unix / Linux中特定文件的递归副本?

                  I tried the answers from following questions: Recursive copy of specific files in Unix/Linux?

                  从所有子目录复制具有特定扩展名的所有文件,但是复制了相同的目录(和子目录)结构。

                  and Copy all files with a certain extension from all subdirectories, but got copied the same directory(and subdirectories) structure.

                  有任何帮助或建议吗?

                  推荐答案

                  使用Java代码,我实现了以下目标:

                  Using java code, i achieved this as follows:

                  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();
                          }
                  

                  这篇关于将特定文件从目录和子目录复制到Mac中的目标文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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