如何在ruby中批量重命名文件 [英] How to mass rename files in ruby

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

问题描述

我一直在尝试制定一个基于ruby的文件重命名程序,作为自己的一个编程练习(我知道linux下的rename,但我想学习Ruby,并且在Mac上没有rename).

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 is not available in Mac).

从下面的代码来看,问题是 .include? 方法总是返回 false,即使我看到文件名包含这样的搜索模式.如果我注释掉 include? 检查,gsub() 似乎根本不会生成新的文件名(即文件名保持不变).那么有人可以看看我做错了什么吗?提前致谢!

From the code below, the issue is that the .include? method always returns false even though I see the filename contains such search pattern. If I comment out the include? check, gsub() does not seem to generate a new file name at all (i.e. file name remains the same). So can someone please take a look at see what I did wrong? Thanks a bunch in advance!

这是预期的行为:假设当前文件夹中有三个文件:a1.jpg、a2.jpg和a3.jpgRuby 脚本应该能够将其重命名为 b1.jpg、b2.jpg、b3.jpg

Here is the expected behavior: Assuming that in current folder there are three files: a1.jpg, a2.jpg, and a3.jpg The Ruby script should be able to rename it to b1.jpg, b2.jpg, b3.jpg

#!/Users/Antony/.rvm/rubies/ruby-1.9.3-p194/bin/ruby

puts "Enter the file search query"
searchPattern = gets
puts "Enter the target to replace"
target = gets
puts "Enter the new target name"
newTarget = gets
Dir.glob("./*").sort.each do |entry|
  origin = File.basename(entry, File.extname(entry))
  if origin.include?(searchPattern)
    newEntry = origin.gsub(target, newTarget)
    File.rename( origin, newEntry )
    puts "Rename from " + origin + " to " + newEntry
  end
end

推荐答案

稍微修改的版本:

puts "Enter the file search query"
searchPattern = gets.strip
puts "Enter the target to replace"
target = gets.strip
puts "Enter the new target name"
newTarget = gets.strip
Dir.glob(searchPattern).sort.each do |entry|
  if File.basename(entry, File.extname(entry)).include?(target)
    newEntry = entry.gsub(target, newTarget)
    File.rename( entry, newEntry )
    puts "Rename from " + entry + " to " + newEntry
  end
end

主要区别:

  • 使用 .strip 删除从 gets 获得的尾随换行符.否则,此换行符会搞乱您的所有匹配尝试.
  • glob 调用中使用用户提供的搜索模式,而不是对所有内容进行通配,然后再手动过滤.
  • 在对gsubrename 的调用中使用entry(即完整的文件名)而不是origin>.origin 实际上只对 .include? 测试有用.由于它是文件名的片段,因此不能与 rename 一起使用.我完全删除了 origin 变量以避免误用它.
  • Use .strip to remove the trailing newline that you get from gets. Otherwise, this newline character will mess up all of your match attempts.
  • Use the user-provided search pattern in the glob call instead of globbing for everything and then manually filtering it later.
  • Use entry (that is, the complete filename) in the calls to gsub and rename instead of origin. origin is really only useful for the .include? test. Since it's a fragment of a filename, it can't be used with rename. I removed the origin variable entirely to avoid the temptation to misuse it.

对于您的示例文件夹结构,为三个输入提示(分别)输入 *.jpgab 应重命名文件如您所愿.

For your example folder structure, entering *.jpg, a, and b for the three input prompts (respectively) should rename the files as you are expecting.

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

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