我正在尝试创建一个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 /“row”.each do | f | #search / Users / e ...
任何想法?
答案 0 :(得分: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