0

我正在尝试创建一个 ruby​​ 脚本,它将打开一个 csv 文件并根据 csv 文件中每一行的内容在目录中搜索文件

e.g

**.csv file**    **directory**
13456            13456.jpg
13567            13456a.jpg
13687            13456b.jpg
                 13567.jpg
                 13687.jpg
                 13687a.jpg

一旦我发现我想将找到的文件复制到一个名为 Keep 的文件夹中

我一直在搞不清楚如何搜索目录

所以我做了一些编码,但我仍然卡住了

 #require 'fileutils'
require 'spreadsheet'

input = "/Users/eccleshall/ImageCleanUp/" #where images are stored
output = "/Users/eccleshall/ImageCleanUp/Keep" #where images will be copied to

book = Spreadsheet.open '/Users/eccleshall/Desktop/ImageCleanUpScript/B002.xls' #opens workbook

sheet1 = book.worksheet 0 # sets worksheet

sheet1.each do |row| #for each row output
    puts row

    Dir.glob("/Users/eccleshall/ImageCleanUp/" row).each do|f| #search /Users/eccleshall/ImageCleanUp/ for files startig with row
        puts f
    end
end

虽然我在运行时不断收到错误

ImageCleanUp.rb:14:语法错误,意外的 tIDENTIFIER,期待 ')' ...s/eccleshall/ImageCleanUp/" 行).each do|f| #search /Users/e...

有什么想法吗?

4

1 回答 1

1

您可以通过以下方式搜索它:

require 'fileutils'
Dir.glob('/path_to_file_directory/*.csv').each do |f|  # you can replace the extension you're looking for.
     # the 'f' will give a string representing the path of each file
end

或者:

Dir.foreach('/path_to_file_directory/') do |i|
  next if i == '.' or i == '..'
  # do something with each file
end 
于 2013-02-21T19:26:44.060 回答