How to mass rename files in ruby

前端 未结 5 1248
梦谈多话
梦谈多话 2021-02-20 02:21

I have been trying to work out a file rename program based on ruby, as a programming exercise for myself (I am aware of rename under linux, but I want to learn Ruby, and rename

5条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-20 03:14

    Here's a short version I've used today (without pattern matching)

    Save this as rename.rb file and run it inside the command prompt with ruby rename.rb

    count = 1
    newname = "car"
    Dir["/path/to/folder/*"].each do |old|
      File.rename(old, newname + count.to_s)
      count += 1
    end
    

    I had /Copy of _MG_2435.JPG converted into car1, car2, ...

提交回复
热议问题