我想使用Ruby从一个文件夹中获取所有文件名。
你还可以选择快捷方式
Dir["/path/to/search/*"]
如果您想在任何文件夹或子文件夹中查找所有的Ruby文件:
Dir["/path/to/search/**/*.rb"]
Dir.entries(folder)
例子:
Dir.entries(".")
来源: http://ruby-doc.org/core/classes/Dir.html#method-c-entries
Dir#glob
),也没有阻止其他人发布一个真正好的答案。当然,我更多地是一个“看到积极方面”的人…… - Mike WoodhouseDir
,每次需要使用它时都需要阅读文档。我在这里发布了我的问题和答案,以便以后能够找到它,并可能帮助有同样问题的人。我认为我听过 SO 播客中说过这种行为没有问题。如果您有更好的答案,请发布。我已经发表了我所知道的内容,我不是 Ruby 忍者。我定期接受得票最多的答案。 - Željko FilipinDir[]
或Dir.glob
更好。当path = '/tmp'
时,比较:Dir.glob("#{path}/*")
与Dir.entries(path)
。返回值略有不同(“。”,“..”),但后者在快速浏览时更容易理解。 - Benjamin Oakes"."
、".."
点文件夹:Dir.entries("your/folder").select { |f| File.file? File.join("your/folder", f) }
...select {|f| File.file? f}
,这样语法更短且意思更明确。 - AutomaticoDir.entries("your/folder").select {|f| File.file? f}
- Automatico!File.directory?
可以工作,但 File.file?
不行。 - Kamil Lelonek.reject {|f| File.directory? f}
看起来比 .select{|f| !File.directory? f}
更简洁。哦,现在我看到了第一个评论...也不错。 - Ian要递归获取所有文件(仅限文件):
Dir.glob('path/**/*').select { |e| File.file? e }
或者任何不是目录的内容(File.file?
会拒绝非常规文件):
Dir.glob('path/**/*').reject { |e| File.directory? e }
使用Find#find
而不是像Dir.glob
这样基于模式的查找方法更好。请参见这个回答“在Ruby中递归列出目录的一行代码解决方案”。
在 Ruby 2.5 中,你现在可以使用 Dir.children
方法。它会返回文件名的数组,但不包括 "." 和 ".."。
例如:
Dir.children("testdir") #=> ["config.h", "main.rb"]
# With a relative path, Dir[] will return relative paths
# as `[ './myfile', ... ]`
#
Dir[ './*' ].select{ |f| File.file? f }
# Want just the filename?
# as: [ 'myfile', ... ]
#
Dir[ '../*' ].select{ |f| File.file? f }.map{ |f| File.basename f }
# Turn them into absolute paths?
# [ '/path/to/myfile', ... ]
#
Dir[ '../*' ].select{ |f| File.file? f }.map{ |f| File.absolute_path f }
# With an absolute path, Dir[] will return absolute paths:
# as: [ '/home/../home/test/myfile', ... ]
#
Dir[ '/home/../home/test/*' ].select{ |f| File.file? f }
# Need the paths to be canonical?
# as: [ '/home/test/myfile', ... ]
#
Dir[ '/home/../home/test/*' ].select{ |f| File.file? f }.map{ |f| File.expand_path f }
# In the current working dir:
#
Dir.entries( '.' ).select{ |f| File.file? f }
# In another directory, relative or otherwise, you need to transform the path
# so it is either absolute, or relative to the current working dir to call File.xxx functions:
#
home = "/home/test"
Dir.entries( home ).select{ |f| File.file? File.join( home, f ) }
[1] 在Unix上,.dotfile
,我不了解Windows的情况。
此代码仅返回带有其扩展名的文件名(不包括全局路径)
Dir.children("/path/to/search/")
=> [文件1.rb、文件2.html、文件3.js]
就我个人而言,我发现这对于在文件夹中循环遍历文件、前向安全非常有用:
Dir['/etc/path/*'].each do |file_name|
next if File.directory? file_name
end
这是一个寻找目录中文件的解决方案:
files = Dir["/work/myfolder/**/*.txt"]
files.each do |file_name|
if !File.directory? file_name
puts file_name
File.open(file_name) do |file|
file.each_line do |line|
if line =~ /banco1/
puts "Found: #{line}"
end
end
end
end
end
.
,..
]和以.
开头的隐藏文件。files = Dir.entries("your/folder").reject {|f| File.directory?(f) || f[0].include?('.')}
Dir.entries
返回的是本地文件名,而不是绝对文件路径。另一方面,File.directory?
期望一个绝对文件路径。这段代码无法按预期工作。 - Nathan
./...
而不是~/
。 - Minh Triet./
表示当前目录,而/
是根挂载点,~/
是用户的主目录。如果您将整个项目移动到其他位置,第一个表示法仍然有效,但其他两个可能会失效。 - mirichan