如何在 Ruby 中重命名文件? [英] How to rename a file in Ruby?

查看:40
本文介绍了如何在 Ruby 中重命名文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的 .rb 文件:

Here's my .rb file:

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, filename.capitalize + File.extname(f))
end

puts "Renaming complete."

文件从它们的初始目录移动到 .rb 文件所在的位置.我想当场重命名文件,而不是移动它们.

The files are moved from their initial directory to where the .rb file is located. I'd like to rename the files on the spot, without moving them.

有什么建议吗?

推荐答案

简单点:

File.rename(f, folder_path + "/" + filename.capitalize + File.extname(f))

这篇关于如何在 Ruby 中重命名文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆