Created
November 15, 2023 14:08
-
-
Save ongaeshi/41ab7e9ff57fe04631d0f366ba5c400f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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