当前档案名称:
Empty_test-one.txt,Empty_test-two.txt,Empty_test-three.txt
我只想重命名单词空。到目前为止我的代码是:
puts "Indicate new name of files":
new_name = gets.chomp
# Look for the specific files
Dir.glob("*.txt").each do |renaming|
# Renaming of files starts, but not on every file
File.rename(renaming, new_name + ".txt") 我目前无法重命名每个单独的文件,并保留文件的第二部分(测试-1,测试-2,测试-3)。
你能帮帮我吗?
发布于 2016-02-19 16:39:09
old_part = "Empty"
puts "Indicate new name of files":
new_name = gets.chomp
# Look for the specific files
Dir.glob("*#{old_part}*.txt").each do |renaming|
full_new_name = renaming.sub(/\A(.*)#{old_part}(.*)\z/, "\\1#{new_name}\\2")
File.rename(renaming, full_new_name)
end您所缺少的是正确构建新的文件名,将old_name更改为new_name。
https://stackoverflow.com/questions/35509914
复制相似问题