如何在ruby中找到不是当前文件的文件的文件路径

时间:2018-04-21 20:38:50

标签: ruby

我有一个文件夹,一些是txt文件,另一些是rb文件。我想要做的是使用main.rb文件中的代码找到其中一个txt文件的路径。

我想一个便宜的技巧是使用File.dirname(__FILE__)来获取当前rb文件的路径,因为txt文件在它工作的同一个文件夹中。但上帝禁止txt文件不在同一个文件夹中,有没有办法找到该txt文件的路径?

1 个答案:

答案 0 :(得分:5)

一个简单的Dir['c:/**/test.txt']将为您提供c:驱动器上所有test.txt文件的数组。

Dir['c:/**/*.txt']将为您提供扩展名为.txt的所有文件(可能很多)

但是在windows中有一个优秀的工具search everything,它还有一个命令行版本,你可以在Ruby脚本中捕获它的输出。在一个大文件夹或驱动器上,这将快得多,并且#34; Dir"或"查找"也可以使用。我曾经这样做过,这里执行的方法是,您需要同时安装所有内容和命令行扩展名。

require 'win32ole'
ES = 'C:\****\es\es.exe' # path to command line of Search Everything

def filelist path
  command = %Q{"#{ES}" -n 60 folder: -p #{path.join(" ").gsub('/','\\')}}
  list = []
  IO.popen(command+" 2>&1") do |pipe|
    while lijn = pipe.gets
      list << lijn.chomp
    end
  end
  list.join(',')
end

修改

对于Gary,一个向操作系统发出攻击的方法,我在我的同步工具中使用它需要文件的最后修改时间并且使用Ruby方法获取它对于超过一千个文件来说太慢了。 它返回一个哈希,其中键是路径,值是文件的最后修改日期。它会根据您的意愿跳过一些文件Adapt。

def list_files path
    folder, collection = "", {}
    IO.popen("dir /s /a:-d #{path}\\*.* 2>&1").each_line do |line|
      case line
        when /$RECYCLE.BIN|AlbumArt/ # skip these
        when /\d{8}T\d{6}/ # skip these
        when /desktop.ini|thumbs.db|sync_hist$/ # skip these
        when /^(\d{2}\/\d{2}\/\d{4}  \d{2}:\d{2})/
          modified = $1
          filename = line[36..-1].chomp
          collection["#{folder}\\#{filename}".downcase] = DateTime::strptime(modified, "%d/%m/%Y  %H:%M") rescue nil
        when /^ Map van / # Dutch for Folder of (my OS is in Dutch)
          folder = line[9..-1].chomp[path.length..-1]
      end
    end
    collection
  end

EDIT2

今天我不得不使用其中一种方法,因为我必须处理的文件夹包含大约30000个文件,并且在正常Ruby Dir发生某些事情之前的等待时间太长而且我的系统在脚本执行时冻结了。

我记得这个答案所以我想包括结果。

我做了一些基准测试,明确的赢家是来自windows self的de dir。 在我首次发布的方法中存在一些错误和附加信息,但我不打算更改它们,因为答案被接受了,并且额外的(例如修改时间)可能是有用的。

在这里我用他们的基准测试的三种方式和第四种使用懒惰来看看有什么变化(不多)。

require 'benchmark' 

STDOUT.sync = true
start_folder = 'c:/jpg'

def ruby_dir folder
  ruby_folder = folder.gsub('\\','/')
  files = []
  Dir.glob("#{ruby_folder}/**/*").each do |file|
    files << file if File.file? file
  end
  files
end

def ruby_dir_with_lazy folder
  ruby_folder = folder.gsub('\\','/')
  files = []
  Dir.glob("#{ruby_folder}/**/*").lazy.each do |file|
    if File.file? file
      files << file
    end
  end
  files
end

def os_dir path
  win_path = path.gsub('/','\\')
  files = []
  folder = win_path
  IO.popen("dir /s /a:-d #{win_path}\\*.* 2>&1").each_line do |line|
    case line
      when /^(\d{2}\/\d{2}\/\d{4}  \d{2}:\d{2})/
        filename = line[36..-1].chomp
        files << "#{folder}\\#{filename}"
      when /^ Map van / # Dutch for Folder of (my OS is in Dutch)
        folder = line[9..-1].chomp
    end
  end
  files
end

def es_dir path
  win_path = path.gsub('/','\\')
  files = []
  es = 'c:\everything\es\es.exe' # path to command line of Search Everything
  command = %Q{"#{es}" -p #{win_path}}
  IO.popen(command+" 2>&1").each_line do |line|
    files << line
  end
  files
end

Benchmark.bm do |x| 
  x.report("ruby_dir          ") { 3.times { ruby_dir(path) } }
  x.report("ruby_dir_with_lazy") { 3.times { ruby_dir_with_lazy(path) } }
  x.report("os_dir            ") { 3.times { os_dir(path) } } 
  x.report("es_dir            ") { 3.times { es_dir(path) } } 
end 

os_dir的结果比标准Ruby Dir快26倍

ruby_dir            1.747000  18.626000  20.373000 ( 20.397883)
ruby_dir_with_lazy  1.482000  18.799000  20.281000 ( 20.340853)
os_dir              0.608000   0.124000   0.732000 (  0.786640)
es_dir              1.202000   1.202000   2.404000 (  5.905093)