热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

获取目录中的文件夹列表-Gettingalistoffoldersinadirectory

HowdoIgetalistofthefoldersthatexistinacertaindirectorywithruby?如何使用ruby获得存在于某个目录中的文

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

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

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

条目()看起来很接近,但我不知道如何只限制文件夹。

12 个解决方案

#1


62  

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

乔丹很接近,但是迪尔。条目没有返回File.directory的完整路径?预计。试试这个:

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

#2


90  

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。glob行也可以传递给Dir。作为一个整体是指:

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

#3


40  

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.

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

#4


18  

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.

注意:Dir。[]Dir.glob等工作。

#5


4  

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:

您可以使用File.directory吗?从FileTest模块查找文件是否为目录。结合Dir。条目是一个不错的(有点)-内衬:

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

Edit: Updated per ScottD's correction.

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

#6


4  

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

#7


1  

Dir.glob('/your_dir').reject {|e| !File.directory?(e)}

#8


1  

$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

#9


0  

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.

我认为您可以测试每个文件,看看它是否是带有FileTest.directory的目录?(file_name)。更多信息请参见文件测试文档。

#10


0  

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).

这将与~/*/(主目录中的所有文件夹)之类的路径一起工作。

#11


0  

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

我们可以把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 }

#12


0  

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

只有文件夹(”。’和‘. .的除外):

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

Dir.glob(文件。加入(文件路径、“*”:分隔符))

Folders and files:

文件夹和文件:

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

Dir.glob(文件。加入(路径,“*”))


推荐阅读
  • Iamtryingtomakeaclassthatwillreadatextfileofnamesintoanarray,thenreturnthatarra ... [详细]
  • 本文讨论了一个关于cuowu类的问题,作者在使用cuowu类时遇到了错误提示和使用AdjustmentListener的问题。文章提供了16个解决方案,并给出了两个可能导致错误的原因。 ... [详细]
  • XML介绍与使用的概述及标签规则
    本文介绍了XML的基本概念和用途,包括XML的可扩展性和标签的自定义特性。同时还详细解释了XML标签的规则,包括标签的尖括号和合法标识符的组成,标签必须成对出现的原则以及特殊标签的使用方法。通过本文的阅读,读者可以对XML的基本知识有一个全面的了解。 ... [详细]
  • Java容器中的compareto方法排序原理解析
    本文从源码解析Java容器中的compareto方法的排序原理,讲解了在使用数组存储数据时的限制以及存储效率的问题。同时提到了Redis的五大数据结构和list、set等知识点,回忆了作者大学时代的Java学习经历。文章以作者做的思维导图作为目录,展示了整个讲解过程。 ... [详细]
  • 阿,里,云,物,联网,net,core,客户端,czgl,aliiotclient, ... [详细]
  • C语言注释工具及快捷键,删除C语言注释工具的实现思路
    本文介绍了C语言中注释的两种方式以及注释的作用,提供了删除C语言注释的工具实现思路,并分享了C语言中注释的快捷键操作方法。 ... [详细]
  • 使用Ubuntu中的Python获取浏览器历史记录原文: ... [详细]
  • Android JSON基础,音视频开发进阶指南目录
    Array里面的对象数据是有序的,json字符串最外层是方括号的,方括号:[]解析jsonArray代码try{json字符串最外层是 ... [详细]
  • 本文详细介绍了Spring的JdbcTemplate的使用方法,包括执行存储过程、存储函数的call()方法,执行任何SQL语句的execute()方法,单个更新和批量更新的update()和batchUpdate()方法,以及单查和列表查询的query()和queryForXXX()方法。提供了经过测试的API供使用。 ... [详细]
  • 本文详细介绍了Java中vector的使用方法和相关知识,包括vector类的功能、构造方法和使用注意事项。通过使用vector类,可以方便地实现动态数组的功能,并且可以随意插入不同类型的对象,进行查找、插入和删除操作。这篇文章对于需要频繁进行查找、插入和删除操作的情况下,使用vector类是一个很好的选择。 ... [详细]
  • 本文讨论了在数据库打开和关闭状态下,重新命名或移动数据文件和日志文件的情况。针对性能和维护原因,需要将数据库文件移动到不同的磁盘上或重新分配到新的磁盘上的情况,以及在操作系统级别移动或重命名数据文件但未在数据库层进行重命名导致报错的情况。通过三个方面进行讨论。 ... [详细]
  • 本文介绍了Android 7的学习笔记总结,包括最新的移动架构视频、大厂安卓面试真题和项目实战源码讲义。同时还分享了开源的完整内容,并提醒读者在使用FileProvider适配时要注意不同模块的AndroidManfiest.xml中配置的xml文件名必须不同,否则会出现问题。 ... [详细]
  • 前景:当UI一个查询条件为多项选择,或录入多个条件的时候,比如查询所有名称里面包含以下动态条件,需要模糊查询里面每一项时比如是这样一个数组条件:newstring[]{兴业银行, ... [详细]
  • Java学习笔记之面向对象编程(OOP)
    本文介绍了Java学习笔记中的面向对象编程(OOP)内容,包括OOP的三大特性(封装、继承、多态)和五大原则(单一职责原则、开放封闭原则、里式替换原则、依赖倒置原则)。通过学习OOP,可以提高代码复用性、拓展性和安全性。 ... [详细]
  • Java在运行已编译完成的类时,是通过java虚拟机来装载和执行的,java虚拟机通过操作系统命令JAVA_HOMEbinjava–option来启 ... [详细]
author-avatar
曾明铭智瑜淑岳
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有