ind*_*ndi 4 ruby file function
我需要一个递归函数来列出文件夹中的文件:
def procdir(dirname)
  data = ''
  Dir.foreach(dirname) do |dir|
    dirpath = dirname + '/' + dir
    if File.directory?(dirpath) then
      if dir != '.' && dir != '..' then
        #puts "DIRECTORY: #{dirpath}" ; 
        procdir(dirpath)
      end
    else
      data += dirpath
    end
  end
  return data
end
但结果是:为空
dbe*_*hur 16
Dir#glob当你给它**全局时,Stdlib会递归.
def procdir(dir)
  Dir[ File.join(dir, '**', '*') ].reject { |p| File.directory? p }
end
使用find模块:
require 'find'
pathes = []
Find.find('.') do |path|
  pathes << path unless FileTest.directory?(path)
end
puts pathes.inspect
旧线程,但也许有人会觉得有用:
array_of_all_files = Dir
  .glob("**/*")
  .reject { |file_path| File.directory? file_path }
| 归档时间: | 
 | 
| 查看次数: | 10276 次 | 
| 最近记录: |