require "fileutils" Dir.mkdir("該当") b = [] a = [] Dir.glob("*/") { |s| t = s.gsub(/(\[.+?\] )?(.+?)( \(.+?\))?\//, '\2') FileUtils.move(s, "該当") if a.include?(t) b << t if a.include?(t) a << t } Dir.glob("*/") { |s| t = s.gsub(/(\[.+?\] )?(.+?)( \(.+?\))?\//, '\2') FileUtils.move(s, "該当") if b.include?(t) } system("explorer 該当")