Rails FileUtils和File.rename不断删除文件

时间:2013-07-25 05:54:46

标签: ruby-on-rails ruby fileutils

我正在尝试遍历图像文件目录并重命名它们。每次运行此方法时,它都会重命名一个文件并删除其余文件。我也用过File.rename。

require 'fileutils'

def rename_image_files
    @last_product = Product.find(:last).id + 1

    Dir.foreach("app/assets/images/products/#{@last_product}").each do |item|
    @count = 0
    @count += 1

    new_name = "#{@last_product.to_s << '_'}#{@count}"
    @item = item
    unless @item == '.' or @item == '..'


    FileUtils.mv("app/assets/images/products/#{@last_product}/#{@item}", "app/assets/images/products/#{@last_product}/#{new_name}.png")


    end
end

0 个答案:

没有答案