快速 Ruby 批量重命名器
我试图将我在这里和这里注意到的几个不同的脚本结合起来 链接文本 尝试获取一个基本脚本,该脚本允许我根据用户给出的目录字符和文件扩展名来删除字符或重命名文件。
我正在努力将它们联系在一起。到目前为止,这就是我所在的地方。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是您的代码的重构。从您的问题或代码中尚不完全清楚,但我假设您想从目录中的每个文件名中删除给定的字符。
请注意,您从博客文章中复制的 strip() 方法完全没有必要,因为它是内置
tr()
方法的糟糕重新实现。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.该程序永远不会调用您的
stripdetails
方法。尝试删除“获取要更改的文件的目录”块上的def stripdetails..
和end
行,以便代码在相同的范围内运行。This program never calls your
stripdetails
method. Try removing thedef stripdetails..
andend
lines on your "Get directory of files to be changed" block, so that code runs in the same scope.