Skip to content

Instantly share code, notes, and snippets.

@motoishmz
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save motoishmz/3be6b31318be68ba0c28 to your computer and use it in GitHub Desktop.
Save motoishmz/3be6b31318be68ba0c28 to your computer and use it in GitHub Desktop.
require "FileUtils"
# 検索結果保存用
matches = []
# ここでフィルター条件をdefine
def filter(filename)
# 拡張子を取る
base_name = File::basename(filename, ".*")
# 数字に変換 (名前を to integer する)
file_num = base_name.to_i
# 6の倍数ならtrue
return (file_num % 6 == 0)
end
# このruby fileと同じdirのファイルに対して一括操作
Dir::entries(".").each{ |frame|
abs_path = File::expand_path(frame)
next if File.ftype(abs_path) != "file"
next if File.basename(abs_path) =~ /^\./
next if File.basename(abs_path) =~ /\.rb$/
# 条件に合ってたら、matchesの中にファイル名を保存
if filter(frame) then
matches.push( abs_path )
end
}
# ./copied フォルダの中を空に
save_folder_path = "./copied"
FileUtils.rm(Dir.glob(save_folder_path+'/*.*'))
FileUtils.mkdir_p(save_folder_path)
# matchした全ファイルをコピー
matches.each{ |src|
dest = save_folder_path + "/" + File::basename(src)
FileUtils.cp( src, dest )
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment