我需要根据用户的指示重命名多个文件的部分名称
I need to rename part of the name of multiple files, as per user's indication
当前文件名:
Empty_test-one.txt,Empty_test-two.txt,Empty_test-three.txt
我只想重命名 Empty 这个词。到目前为止我的代码:
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")
我目前无法重命名每个单独的文件并保留文件的第二部分(测试一、测试二、测试三)。
你能帮帮我吗?
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
。
当前文件名:
Empty_test-one.txt,Empty_test-two.txt,Empty_test-three.txt
我只想重命名 Empty 这个词。到目前为止我的代码:
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")
我目前无法重命名每个单独的文件并保留文件的第二部分(测试一、测试二、测试三)。
你能帮帮我吗?
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
。