获取目录中的文件夹列表

发布于 2024-08-14 04:09:03 字数 93 浏览 11 评论 0原文

如何使用 ruby​​ 获取某个目录中存在的文件夹列表?

Dir.entries() 看起来很接近,但我不知道如何仅限于文件夹。

How do I get a list of the folders that exist in a certain directory with ruby?

Dir.entries() looks close but I don't know how to limit to folders only.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

述情 2024-08-21 04:09:03

我发现这更有用且易于使用:

Dir.chdir('/destination_directory')
Dir.glob('*').select {|f| File.directory? f}

它获取当前目录中的所有文件夹,排除 ...

要递归文件夹,只需使用 ** 代替 *

Dir.glob 行也可以作为块传递给 Dir.chdir

Dir.chdir('/destination directory') do
  Dir.glob('*').select { |f| File.directory? f }
end

I've found this more useful and easy to use:

Dir.chdir('/destination_directory')
Dir.glob('*').select {|f| File.directory? f}

it gets all folders in the current directory, excluded . and ...

To recurse folders simply use ** in place of *.

The Dir.glob line can also be passed to Dir.chdir as a block:

Dir.chdir('/destination directory') do
  Dir.glob('*').select { |f| File.directory? f }
end
浮萍、无处依 2024-08-21 04:09:03

Jordan 很接近,但是 Dir.entries 没有返回 File.directory? 期望的完整路径。试试这个:

 Dir.entries('/your_dir').select {|entry| File.directory? File.join('/your_dir',entry) and !(entry =='.' || entry == '..') }

Jordan is close, but Dir.entries doesn't return the full path that File.directory? expects. Try this:

 Dir.entries('/your_dir').select {|entry| File.directory? File.join('/your_dir',entry) and !(entry =='.' || entry == '..') }
ま昔日黯然 2024-08-21 04:09:03

在我看来,Pathname 比普通字符串更适合文件名。

require "pathname"
Pathname.new(directory_name).children.select { |c| c.directory? }

这将为您提供该目录中所有目录的数组作为 Pathname 对象。

如果你想要字符串

Pathname.new(directory_name).children.select { |c| c.directory? }.collect { |p| p.to_s }

如果directory_name是绝对的,那么这些字符串也是绝对的。

In my opinion Pathname is much better suited for filenames than plain strings.

require "pathname"
Pathname.new(directory_name).children.select { |c| c.directory? }

This gives you an array of all directories in that directory as Pathname objects.

If you want to have strings

Pathname.new(directory_name).children.select { |c| c.directory? }.collect { |p| p.to_s }

If directory_name was absolute, these strings are absolute too.

稍尽春風 2024-08-21 04:09:03

递归查找某个目录下的所有文件夹:

Dir.glob 'certain_directory/**/*/'

非递归版本:

Dir.glob 'certain_directory/*/'

注意:Dir.[] 的工作方式类似于 Dir.glob

Recursively find all folders under a certain directory:

Dir.glob 'certain_directory/**/*/'

Non-recursively version:

Dir.glob 'certain_directory/*/'

Note: Dir.[] works like Dir.glob.

幸福不弃 2024-08-21 04:09:03

有了这个,您可以在一个目录中获取目录子目录子子目录完整路径数组。递归方式。
我使用该代码将这些文件加载​​到 config/application 文件中。

Dir.glob("path/to/your/dir/**/*").select { |entry| File.directory? entry }

此外,我们不再需要处理无聊的 ... 了。接受的答案需要处理它们。

With this one, you can get the array of a full path to your directories, subdirectories, subsubdirectories in a recursive way.
I used that code to eager load these files inside config/application file.

Dir.glob("path/to/your/dir/**/*").select { |entry| File.directory? entry }

In addition we don't need deal with the boring . and .. anymore. The accepted answer needed to deal with them.

执手闯天涯 2024-08-21 04:09:03

您可以使用 FileTest 模块中的 File.directory? 来确定文件是否是目录。将此与 Dir.entries 结合起来,形成一个不错的单行:

directory = 'some_dir'
Dir.entries(directory).select { |file| File.directory?(File.join(directory, file)) }

编辑: 根据 ScottD 的更正进行更新。

You can use File.directory? from the FileTest module to find out if a file is a directory. Combining this with Dir.entries makes for a nice one(ish)-liner:

directory = 'some_dir'
Dir.entries(directory).select { |file| File.directory?(File.join(directory, file)) }

Edit: Updated per ScottD's correction.

吾家有女初长成 2024-08-21 04:09:03
directory = 'Folder'
puts Dir.entries(directory).select { |file| File.directory? File.join(directory, file)}
directory = 'Folder'
puts Dir.entries(directory).select { |file| File.directory? File.join(directory, file)}
你好,陌生人 2024-08-21 04:09:03
$dir_target = "/Users/david/Movies/Camtasia 2/AzureMobileServices.cmproj/media"

Dir.glob("#{$dir_target}/**/*").each do |f| 
  if File.directory?(f)
    puts "#{f}\n"
  end
end
$dir_target = "/Users/david/Movies/Camtasia 2/AzureMobileServices.cmproj/media"

Dir.glob("#{$dir_target}/**/*").each do |f| 
  if File.directory?(f)
    puts "#{f}\n"
  end
end
无声静候 2024-08-21 04:09:03
Dir.glob('/your_dir').reject {|e| !File.directory?(e)}
Dir.glob('/your_dir').reject {|e| !File.directory?(e)}
青瓷清茶倾城歌 2024-08-21 04:09:03

对于通用解决方案,您可能想要使用

Dir.glob(File.expand_path(path))

这将适用于像 ~/*/ 这样的路径(您的主目录中的所有文件夹)。

For a generic solution you probably want to use

Dir.glob(File.expand_path(path))

This will work with paths like ~/*/ (all folders within your home directory).

吃素的狼 2024-08-21 04:09:03

我们可以结合 Borh 的答案johannes 的回答得到了一个相当优雅的解决方案来获取文件夹中的目录名称。

# user globbing to get a list of directories for a path
base_dir_path = ''
directory_paths = Dir.glob(File.join(base_dir_path, '*', ''))

# or recursive version:
directory_paths = Dir.glob(File.join(base_dir_path, '**', '*', ''))

# cast to Pathname
directories = directory_paths.collect {|path| Pathname.new(path) }

# return the basename of the directories
directory_names = directories.collect {|dir| dir.basename.to_s }

We can combine Borh's answer and johannes' answer to get quite an elegant solution to getting the directory names in a folder.

# user globbing to get a list of directories for a path
base_dir_path = ''
directory_paths = Dir.glob(File.join(base_dir_path, '*', ''))

# or recursive version:
directory_paths = Dir.glob(File.join(base_dir_path, '**', '*', ''))

# cast to Pathname
directories = directory_paths.collect {|path| Pathname.new(path) }

# return the basename of the directories
directory_names = directories.collect {|dir| dir.basename.to_s }
梦开始←不甜 2024-08-21 04:09:03

仅文件夹(排除“.”和“..”):

Dir.glob(File.join(path, "*", File::SEPARATOR))

文件夹和文件:

Dir .glob(File.join(路径, "*"))

Only folders ('.' and '..' are excluded):

Dir.glob(File.join(path, "*", File::SEPARATOR))

Folders and files:

Dir.glob(File.join(path, "*"))

暗恋未遂 2024-08-21 04:09:03

我想你可以测试每个文件,看看它是否是一个带有 FileTest.directory 的目录? (文件名)。有关详细信息,请参阅 FileTest 文档

I think you can test each file to see if it is a directory with FileTest.directory? (file_name). See the documentation for FileTest for more info.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文