嵌套目录搜索

Nested directory searching

我正在尝试制作一个程序来搜索 C:\ 中的每个目录、子目录、子子目录等等。我觉得我可以处理那部分,但还有文件夹名称的问题。当我的程序搜索 Foo 或巨大的 if/elsecase 语句时,可能会出现一些案例问题搜索条件。

我的问题是:1.有没有办法忽略字母大小写?和 2. 有没有一种方法可以使搜索语句更有效?

我当前的代码:

#foldersniffer by Touka, ©2015
base = Dir.entries("C:\")
trees = Dir.entries("#{base}")
trees.each do |tree|
    if Dir.exist?("Foo")
        puts "Found Folder \"Foo\" in C:\"
    elsif Dir.exist?("Bar")
        puts "Found Folder \"Bar\" in C:\"
    else
        puts "No folders found"
    end
end
sleep

感谢任何帮助。

编辑:它正在尝试扫描像 bootmgr 这样的文件,但它给我错误...我不确定如何解决这个问题。

我不够专业,无法确定,但我会调查 File::FNM_CASEFOLD

https://lostechies.com/derickbailey/2011/04/14/case-insensitive-dir-glob-in-ruby-really-it-has-to-be-that-cryptic/

考虑使用 Dir.glob(...) 和正则表达式进行不区分大小写的匹配:

Dir.glob('c:\**\*') do |filename|
  if filename =~ /c:\(foo|bar)($|\)/i
    puts "Found #{filename}"
  end
end

Dir.glob 参数区分大小写可能与 Windows 系统无关:

Note that this pattern is not a regexp, it’s closer to a shell glob. See File.fnmatch for the meaning of the flags parameter. Note that case sensitivity depends on your system (so File::FNM_CASEFOLD is ignored), as does the order in which the results are returned.