代码之家  ›  专栏  ›  技术社区  ›  0xC0DEFACE

获取目录中的文件夹列表

  •  79
  • 0xC0DEFACE  · 技术社区  · 15 年前

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

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

    12 回复  |  直到 7 年前
        1
  •  63
  •   the Tin Man    11 年前

    约旦很近,但是 Dir.entries 不返回完整路径 File.directory? 预期。试试这个:

     Dir.entries('/your_dir').select {|entry| File.directory? File.join('/your_dir',entry) and !(entry =='.' || entry == '..') }
    
        2
  •  96
  •   mdesantis    7 年前

    我发现这个更有用,也更容易使用:

    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
    
        3
  •  44
  •   the Tin Man    11 年前

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

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

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

    如果你想有字符串

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

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

        4
  •  19
  •   the Tin Man    11 年前

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

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

    非递归版本:

    Dir.glob 'certain_directory/*/'
    

    注: Dir.[] 工作方式 Dir.glob .

        5
  •  4
  •   Jordan Running    15 年前

    你可以使用 File.directory? FileTest 模块以确定文件是否为目录。将此与 Dir.entries 做一个漂亮的(ish)衬里:

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

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

        6
  •  4
  •   Markus    15 年前
    directory = 'Folder'
    puts Dir.entries(directory).select { |file| File.directory? File.join(directory, file)}
    
        7
  •  1
  •   msangui    12 年前
    Dir.glob('/your_dir').reject {|e| !File.directory?(e)}
    
        8
  •  1
  •   David Douglas    11 年前
    $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
  •  1
  •   thisismydesign    8 年前

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

    Dir.glob(File.expand_path(path))
    

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

        10
  •  0
  •   the Tin Man    11 年前

    我想你可以测试每个文件,看看它是否是一个目录 FileTest.directory? (file_name) . 见 the documentation for FileTest 更多信息。

        11
  •  0
  •   br3nt    7 年前

    我们可以结合 Borh's answer johannes' answer 得到一个非常优雅的解决方案来获取文件夹中的目录名。

    # 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
  •   Iwan B.    7 年前

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

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

    文件夹和文件:

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

    推荐文章