Skip to content

Instantly share code, notes, and snippets.

@ongaeshi
Created November 15, 2023 14:08
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 ongaeshi/41ab7e9ff57fe04631d0f366ba5c400f to your computer and use it in GitHub Desktop.
Save ongaeshi/41ab7e9ff57fe04631d0f366ba5c400f to your computer and use it in GitHub Desktop.
def get_all_folders_recursive(directory)
# ディレクトリ内の全エントリを取得
entries = Dir.entries(directory)
# フォルダのみを抽出
folders = entries.select { |entry| File.directory?(File.join(directory, entry)) && entry != '.' && entry != '..' }
# 現在のディレクトリ内のフォルダをリストに追加
result = folders.map { |folder| File.join(directory, folder) }
# 再帰的にサブディレクトリ内のフォルダも取得
folders.each do |folder|
subdirectory = File.join(directory, folder)
result += get_all_folders_recursive(subdirectory)
end
return result
end
# ディレクトリのパスを指定
directory_path = '/path/to/your/directory'
# フォルダ名を取得
folders = get_all_folders_recursive(directory_path)
# 結果を表示
puts "Folders in #{directory_path} and its subdirectories:"
puts folders
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment