【发布时间】:2013-06-01 09:21:42
【问题描述】:
folder_to_analyze = ARGV.first
folder_path = File.join(Dir.pwd, folder_to_analyze)
unless File.directory?(folder_path)
puts "Error: #{folder_path} no es un folder valido."
exit
end
def get_csv_file_paths(path)
files = []
Dir.glob(path + '/**/*.csv').each do |f|
files << f
end
return files
end
def get_xlsx_file_path(path)
files = []
Dir.glob(path + '/**/*.xls').each do |f|
files << f
end
return files
end
files_to_process = []
files_to_process << get_csv_file_paths(folder_path)
files_to_process << get_xlsx_file_path(folder_path)
puts files_to_process[1].length # Not what I want, I want:
# puts files_to_process.length
我正在尝试在 Ruby 中创建一个简单的脚本,允许我从命令行调用它,例如 ruby counter.rb mailing_list1,它会转到文件夹并计算所有 .csv 和 .xls 文件。
我打算对每个文件进行操作,获取行数等。
目前files_to_process 数组实际上是一个数组数组——我不希望这样。我想要一个包含 .csv 和 .xls 文件的数组。
由于我不知道如何从 Dir.glob 调用中产生,我将它们添加到一个数组并返回。
如何使用单个数组完成此操作?
【问题讨论】: