How to rename a file in Ruby?

后端 未结 5 1047
南方客
南方客 2020-12-13 03:22

Here\'s my .rb file:

puts \"Renaming files...\"

folder_path = \"/home/papuccino1/Desktop/Test\"
Dir.glob(folder_path + \"/*\").sort.each do |f|
    filename         


        
5条回答
  •  离开以前
    2020-12-13 04:01

    Doesn't the folder_path have to be part of the filename?

    puts "Renaming files..."
    
    folder_path = "/home/papuccino1/Desktop/Test/"
    Dir.glob(folder_path + "*").sort.each do |f|
      filename = File.basename(f, File.extname(f))
      File.rename(f, folder_path + filename.capitalize + File.extname(f))
    end
    
    puts "Renaming complete."
    

    edit: it appears Mat is giving the same answer as I, only in a slightly different way.

提交回复
热议问题