【问题标题】:How do I copy specific file types with Maid?如何使用 Maid 复制特定的文件类型?
【发布时间】:2014-09-23 06:44:07
【问题描述】:

我是第一次使用 Ruby 的用户(任何相关的编程代码),我正在尝试在“maid”中创建一个脚本,将我的所有音乐从特定文件夹复制到“自动添加到 iTunes”中文件夹:

rule 'Move Downloaded Music to iTunes' do
    FileUtils.cp_r '/Users/*********/Movies/*********/Music/.',
    '/Users/*********/Music/iTunes/iTunes Media/Automatically Add to iTunes/',
    :remove_destination => true
  end

但是,我在同一个文件夹中有非音乐文件,我只想包含音频格式(mp3、m4a 等)的文件

如何附加此代码以便选择要复制的文件类型?

另外,cp_r和cp有什么区别?

非常欢迎对我的代码提出任何建议或改进 - 我看到人们尝试用更复杂的代码做类似的事情,所以从某种意义上说,我担心我的代码太简单了......谢谢你的帮助!!

【问题讨论】:

    标签: ruby copy fileutils maid


    【解决方案1】:

    您可以使用Dir::glob 查找文件并使用FileUtils::mv 移动它们:

    require 'fileutils'
    
    Dir.glob('/Users/.../Music/*.{mp3,m4a}') do |filename|
      FileUtils.mv(filename, '/Users/.../Automatically Add to iTunes/')
    end
    

    【讨论】:

      【解决方案2】:

      Maid provides some helper methods 会对此有所帮助。让我们将它们单独拼凑起来。

      # Find all files in your Downloads
      dir('~/Downloads/*')
      
      # Find **any kind of audio files** in your Downloads
      where_content_type(dir('~/Downloads/*'), 'audio')
      
      # Copy these specific MP3 files in Downloads to a specific iTunes folder
      copy(['~/Downloads/song_1.mp3', '~/Downloads/song_2.mp3'],
           '~/Music/iTunes/iTunes Media/Automatically Add to iTunes/')
      
      # Copy any MP3 files in Downloads to a specific iTunes folder
      copy(dir('~/Downloads/*.mp3'),
           '~/Music/iTunes/iTunes Media/Automatically Add to iTunes/')
      

      我们可以将这些部分放在一起以实现完整意图,即将音频文件复制到“自动添加到 iTunes”文件夹。

      Maid.rules do
        rule 'copy audio files to the "Automatically Add to iTunes" folder' do
          copy(where_content_type(dir('~/Downloads/*'), 'audio'),
             '~/Music/iTunes/iTunes Media/Automatically Add to iTunes/')
        end
      end
      

      如果您愿意,可以使用变量对其进行分解——毕竟它是 Ruby。

      Maid.rules do
        rule 'copy audio files to the "Automatically Add to iTunes" folder' do
          files_in_downloads = dir('~/Downloads/*')
          audio_files_in_downloads = where_content_type(files_in_downloads, 'audio')
          automatically_add_to_itunes_folder = '~/Music/iTunes/iTunes Media/Automatically Add to iTunes/'
      
          copy(audio_files_in_downloads, automatically_add_to_itunes_folder)
        end
      end
      

      我希望这会有所帮助!

      【讨论】:

        【解决方案3】:

        要复制特殊类型的文件,请使用

        Dir.foreach(absolute_path) do |file|
          if file.downcase.end_with?('.mp3', 'mp4', ....)
              FileUtils.cp(file, final_dir)
          end
        end
        

        遍历所有文件夹以选择可以复制的文件,而不是通过代码复制所有文件夹。

        Ps:cp_rcp在控制台中等于cp -rcp,意思是复制一个文件夹或者只复制一个文件。

        【讨论】:

        • 复制时有没有办法保留文件所在的文件夹?每个音乐文件都位于 src 文件夹中基于艺术家的文件夹中。看来我的代码并没有防止重复副本......
        • 是的,FileUtils.cp_rFileUtils.cp 只是复制文件夹或文件而不是删除源,除非使用 FileUtils.mv_rFileUtils.mv
        猜你喜欢
        • 2020-12-28
        • 1970-01-01
        • 1970-01-01
        • 2023-03-26
        • 2011-01-31
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多