Ian*_*les 507
您还有快捷方式选项
Dir["/path/to/search/*"]
Run Code Online (Sandbox Code Playgroud)
如果你想在任何文件夹或子文件夹中找到所有Ruby文件:
Dir["/path/to/search/**/*.rb"]
Run Code Online (Sandbox Code Playgroud)
Žel*_*pin 163
Dir.entries(folder)
Run Code Online (Sandbox Code Playgroud)
例:
Dir.entries(".")
Run Code Online (Sandbox Code Playgroud)
资料来源:http://ruby-doc.org/core/classes/Dir.html#method-c-entries
Emi*_*ggi 89
下面的代码片段恰好显示了目录里的文件,跳过子目录和名称".",".."点文件夹:
Dir.entries("your/folder").select {|f| !File.directory? f}
Run Code Online (Sandbox Code Playgroud)
kon*_*box 34
以递归方式获取所有文件(仅限严格文件):
Dir.glob('path/**/*').select{ |e| File.file? e }
Run Code Online (Sandbox Code Playgroud)
或者任何不是目录的东西(File.file?会拒绝非常规文件):
Dir.glob('path/**/*').reject{ |e| File.directory? e }
Run Code Online (Sandbox Code Playgroud)
使用Find#find基于模式的查找方法Dir.glob实际上更好.看到这个回答"在Ruby中递归列出目录的一行代码?" .
小智 15
这对我有用:
如果您不想要隐藏文件[1],请使用Dir []:
# 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 }
Run Code Online (Sandbox Code Playgroud)
现在,Dir.entries将返回隐藏文件,并且您不需要通配符asterix(您可以只传递带有目录名的变量),但它将直接返回基本名称,因此File.xxx函数将不起作用.
# 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 ) }
Run Code Online (Sandbox Code Playgroud)
[1] .dotfile关于unix,我不了解Windows
Mar*_*rez 10
在Ruby 2.5中,您现在可以使用Dir.children。它以“。”以外的形式获取文件名作为数组。和“ ..”
例:
Dir.children("testdir") #=> ["config.h", "main.rb"]
Run Code Online (Sandbox Code Playgroud)
http://ruby-doc.org/core-2.5.0/Dir.html#method-c-children
就个人而言,我发现这对于循环文件夹中的文件最有用,前瞻性安全:
Dir['/etc/path/*'].each do |file_name|
next if File.directory? file_name
end
Run Code Online (Sandbox Code Playgroud)
这是在目录中查找文件的解决方案:
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
Run Code Online (Sandbox Code Playgroud)
在获取目录中的所有文件名时,此代码段可用于拒绝目录[ .,..]和以。开头的隐藏文件。.
files = Dir.entries("your/folder").reject {|f| File.directory?(f) || f[0].include?('.')}
Run Code Online (Sandbox Code Playgroud)
这对我有用:
Dir.entries(dir).select { |f| File.file?(File.join(dir, f)) }
Run Code Online (Sandbox Code Playgroud)
Dir.entries返回一个字符串数组。然后,我们必须提供文件的完整路径到File.file?,除非dir等于我们当前的工作目录。这就是为什么这个File.join()。