2013-02-21 47 views
0

我想創建一個ruby腳本,它將打開一個csv文件並根據csv文件中每行的內容搜索一個目錄以查找文件在文件目錄中搜索具有相同起始字符的文件或多個文件

e.g 

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

一旦我發現,我會再像副本中找到一個叫做文件夾中的文件保持

林陷入搞清楚如何搜索目錄

所以香港專業教育學院做了一些編碼但我仍然堅持

#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 /」行)。每個辦| F | #search/Users/e ...

任何想法的人?

+1

你看看查找和文件類? – 2013-02-21 19:26:46

回答

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 
相關問題