Rename specific files depending on a diferent file in same directory
I'm practising some programming and I'm now faced with the following issue. I have a folder with multiple subfolders inside. Each subfolder contains two files: an .xlsx
and a .doc
file. I want to rename the .xlsx
depending on the name of the .doc
file. For example, in directory documents\main_folder\folder_1
there are two files: test_file.xlsx
and final_file.doc
. After running my code, result should be final_file.xlsx
and final_file.doc
. This must happen with all subfolders.
My code so far:
require 'FileUtils'
filename = nil
files = Dir.glob('**/*.doc')
files.each do |rename|
filename = File.basename(rename, File.extname(rename))
puts "working with file: #{filename}"
end
subs = Dir.glob('**/*.xlsx')
subs.each do |renaming|
File.rename(renaming, filename)
end
Two issues with this code: firstly, the .xlsx
is moved where the .rb
file is located. Secondly, renaming is partially achieved, only that the extension is not kept, but completely removed. Any help?
from Recent Questions - Stack Overflow https://ift.tt/3kpVlq7
https://ift.tt/eA8V8J
Comments
Post a Comment