快速 Ruby 批量重命名器

发布于 2024-10-11 11:43:55 字数 996 浏览 2 评论 0原文

我试图将我在这里和这里注意到的几个不同的脚本结合起来 链接文本 尝试获取一个基本脚本,该脚本允许我根据用户给出的目录字符和文件扩展名来删除字符或重命名文件。

我正在努力将它们联系在一起。到目前为止,这就是我所在的地方。

    require 'fileutils'

define renamer(strip, stripdetails) 
# So this is a strip function.

    def strip(str,char)
   new_str = ""
   str.each_byte do |byte|
      new_str << byte.chr unless byte.chr == char
   end
   new_str
end
# and then retrieve details from user.

#Get directory of files to be changed.
def stripdetails(strip myname)
 puts "Enter Directory containing files"
 STDOUT.flush
 oldname = gets.chomp
 puts "what characters do you want to remove"
 str = gets.chomp
 puts "what file extension do files end in?"
 fileXt = gets.chomp
 end

#And I found this from stackoverflow(I don't have enuff credits to post another hyperlink)
old_file = "oldname"
new_file = strip(oldname,str)
FileUtils.mv(old_file, new_file)

I am trying to tie in a few different scripts I have noticed here and here link text
Trying to get a basic script that will allow me to strip characters or rename files given a directory characters and file extension by user.

I am struggling to tie it all together. This is where I am so far.

    require 'fileutils'

define renamer(strip, stripdetails) 
# So this is a strip function.

    def strip(str,char)
   new_str = ""
   str.each_byte do |byte|
      new_str << byte.chr unless byte.chr == char
   end
   new_str
end
# and then retrieve details from user.

#Get directory of files to be changed.
def stripdetails(strip myname)
 puts "Enter Directory containing files"
 STDOUT.flush
 oldname = gets.chomp
 puts "what characters do you want to remove"
 str = gets.chomp
 puts "what file extension do files end in?"
 fileXt = gets.chomp
 end

#And I found this from stackoverflow(I don't have enuff credits to post another hyperlink)
old_file = "oldname"
new_file = strip(oldname,str)
FileUtils.mv(old_file, new_file)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

却一份温柔 2024-10-18 11:43:55

这是您的代码的重构。从您的问题或代码中尚不完全清楚,但我假设您想从目录中的每个文件名中删除给定的字符。

请注意,您从博客文章中复制的 strip() 方法完全没有必要,因为它是内置 tr() 方法的糟糕重新实现。

#Given a directory, renames each file by removing
#specified characters from each filename

require 'fileutils'

puts "Enter Directory containing files"
STDOUT.flush
dir = gets.chomp
puts "what characters do you want to remove from each filename?"
remove = gets.chomp
puts "what file extension do the files end in?"
fileXt = gets.chomp

files = File.join(dir, "*.#{fileXt}")
Dir[files].each do |file|
  new_file = file.tr(remove,"")
  FileUtils.mv(file, new_file)
end

Here's a refactoring of your code. It's not entirely clear from your question or your code, but I'm assuming you want to remove given characters from each filename in a directory.

Note that strip() method you copied from a blog post is entirely unnecessary, as it is a poor reimplementation of the built-in tr() method.

#Given a directory, renames each file by removing
#specified characters from each filename

require 'fileutils'

puts "Enter Directory containing files"
STDOUT.flush
dir = gets.chomp
puts "what characters do you want to remove from each filename?"
remove = gets.chomp
puts "what file extension do the files end in?"
fileXt = gets.chomp

files = File.join(dir, "*.#{fileXt}")
Dir[files].each do |file|
  new_file = file.tr(remove,"")
  FileUtils.mv(file, new_file)
end
娇妻 2024-10-18 11:43:55

该程序永远不会调用您的 stripdetails 方法。尝试删除“获取要更改的文件的目录”块上的 def stripdetails..end 行,以便代码在相同的范围内运行。

This program never calls your stripdetails method. Try removing the def stripdetails.. and end lines on your "Get directory of files to be changed" block, so that code runs in the same scope.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文