Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing file extension using ruby

I have a list of .eml files which are in a remote folder say

\\abcremote\pickup

I want to rename all the files from

xyz.eml to xyz.html

Could you guys help me do that using ruby.

Thanks in advance.

like image 615
Boon Avatar asked Feb 21 '13 11:02

Boon


People also ask

How do I change the extension of a Ruby file?

You can make RubyMine the default application for opening specific file types from the default file manager on your operating system. Press Ctrl+Alt+S to open the IDE settings and select Editor | File Types. Click Associate File Types with RubyMine and select the file extensions you want to open with the IDE.

How do you change a file extension?

You can also do it by right-clicking on the unopened file and clicking on the “Rename” option. Simply change the extension to whatever file format you want and your computer will do the conversion work for you.

What is the file extension for Ruby?

An RB file is a software program written in Ruby, an object-oriented scripting language. Ruby is designed to be simple, efficient, and easy to read. RB files can be edited with a text editor and run using Ruby.


2 Answers

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

like image 198
fmendez Avatar answered Sep 18 '22 12:09

fmendez


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
like image 28
knut Avatar answered Sep 21 '22 12:09

knut