Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Shinya131/7806fe22afb674b24697 to your computer and use it in GitHub Desktop.
Save Shinya131/7806fe22afb674b24697 to your computer and use it in GitHub Desktop.
#require 'pry'
#require 'pp'
# 指定したディレクトリをrootとしてroot以下にあるファイルすべてについて、
# ディレクトリ分けが無視されて、ファイル名だけで区別されるようになった場合に、ファイル名の重複が起きないかチェックする。
#
# e.x.
# # 実際のディレクトリ/ファイル構成
# root/
# root/category_a/
# root/category_a/a_1.txt
# root/category_a/a_2.txt
# root/category_b/
# root/category_b/b_1.txt
# root/category_b/b_2.txt
#
# # この状態になった時に重複しないかチェックする
# root/
# a_1.txt
# a_2.txt
# b_1.txt
# b_2.txt
#
# FilePathに対する極薄いラッパ
class FilePath
def initialize(file_path)
@file_path = File.absolute_path(file_path) # 相対パスを絶対パスにしておく
end
def file_path
@file_path
end
def basename
File.basename(@file_path)
end
end
root_path_list = ARGV
# 指定パス以下のファイルパスを全部探してきてラップをかぶせる
@file_paths = []
root_path_list.each do |root|
unless File.exist?(root)
raise "No such file or directory: #{root}"
end
Dir.glob("#{root}/**/*.*").each do |path|
@file_paths << FilePath.new(path)
end
end
# 重複を探す
duplicate_file_paths = []
@file_paths.each do |path|
@file_base_names ||= @file_paths.map(&:basename) # メモ化
if @file_base_names.count(path.basename) > 1 # duplicate
duplicate_file_paths << path
end
end
# 結果表示
puts "\n# duplicate file names"
duplicate_file_paths.group_by(&:basename).each do |file_name, duplicate_physical_paths|
next if file_name =~ /.DAT/
puts "\n## #{file_name}"
puts "```"
duplicate_physical_paths.each do |path|
puts path.file_path
end
puts "```"
end
#binding.pry
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment