18
17

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Ruby | ディレクトリの一覧取得

Posted at

Ruby | ディレクトリの一覧取得

概要

Rubyでディレクトリの一覧取得

サンプルコード(カレント配下のディレクトリのみ取得)

dirs.rb
%w{hoge hige hage}.each { |dir|Dir.mkdir(dir) unless Dir.exist?(dir) }
print Dir.glob('*/')

出力

["hage/", "hige/", "hoge/"]

サンプルコード(カレント配下のサブディレクトリも含めた全ディレクトリを取得)

dirs_rec.rb
require 'fileutils'
require 'pp'
%w{hoge/hoge/hoge hige hage/hage/hage hage/hege/huge}.each { |dir|FileUtils.mkdir_p(dir) unless Dir.exist?(dir) }
pp Dir.glob('**/')

出力

["hage/",
 "hage/hage/",
 "hage/hage/hage/",
 "hage/hege/",
 "hage/hege/huge/",
 "hige/",
 "hoge/",
 "hoge/hoge/",
 "hoge/hoge/hoge/"]

感謝の意

1つ目のサンプルは ryosy383 さん
2つ目のサンプルは cielavenir さん
に教えていただきました。
ありがとうございます!

18
17
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
18
17

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?