进出口使用搜索在当前目录下只有
Find.find("c:\\test")
搜索的目录文件。我只想在这个级别搜索dir,因此c:\ test中的任何目录都不会被搜索到。
是否有另一种方法可以使用?
感谢
进出口使用搜索在当前目录下只有
Find.find("c:\\test")
搜索的目录文件。我只想在这个级别搜索dir,因此c:\ test中的任何目录都不会被搜索到。
是否有另一种方法可以使用?
感谢
# Temporarily make c:\test your current directory
Dir.chdir('c:/test') do
# Get a list of file names just in this directory as an array of strings
Dir['*'].each do |filename|
# ...
end
end
或者:
# Get a list of paths like "c:/test/foo.txt"
Dir['c:/test/*'] do |absolute|
# Get just the filename, e.g. "foo.txt"
filename = File.basename(absolute)
# ...
end
有了这两个,你可以如果你喜欢,只需将文件名写入数组中:
files = Dir.chdir('c:/text'){ Dir['*'] }
files = Dir['c:/text/*'].map{ |f| File.basename(f) }
Find
的prune
方法,可以跳过当前的文件或目录:
跳过当前文件或目录, 重新启动下一个 进入循环。如果当前文件是 目录,则该目录将不会被递归地输入 。仅在 与 Find :: find关联的块内有意义。
Find.find("c:\\test") do |path|
if FileTest.directory?(path)
Find.prune # Don't look any further into this directory.
else
# path is not a directory, so must be file under c:\\test
# do something with file
end
end
您可以使用Dir.foreach()
,例如,列出在c的所有文件:\测试
Dir.foreach("c:\\test") {|x| puts "#{x}" }