Changing file extension using ruby
Simpler
'abc . . def.mp3'.sub /\.[^\.]+$/, '.opus'
Improving the previous answer a little:
require 'fileutils'
Dir.glob('/path_to_file_directory/*.eml').each do |f|
FileUtils.mv f, "#{File.dirname(f)}/#{File.basename(f,'.*')}.html"
end
The File.basename(f,'.*')
will give you the name without the extension otherwise the files will endup being file_name.eml.html instead of file_name.html
Rake offers a simple command to change the extension:
require 'rake'
p 'xyz.eml'.ext('html') # -> xyz.html
Improving the previous answers again a little:
require 'rake'
require 'fileutils'
Dir.glob('/path_to_file_directory/*.eml').each do |filename|
FileUtils.mv( filename, filename.ext("html"))
end
Pathname has the sub_ext()
method to replace the extension, as well as glob()
and rename()
, allowing the accepted answer to be rewritten a little more compactly:
require 'pathname'
Pathname.glob('/path_to_file_directory/*.eml').each do |p|
p.rename p.sub_ext(".html")
end