# Exchanging the extension in a filename

**URL:** <https://www.ruby-forum.com/t/exchanging-the-extension-in-a-filename/112024>\
**Category:** Ruby\
**Created:** [August 14, 2007, 12:06pm UTC](https://www.ruby-forum.com/t/exchanging-the-extension-in-a-filename/112024 "2007-08-14T12:06:22Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![ronaldfischer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/67e7ee/32.png) [@ronaldfischer](https://www.ruby-forum.com/u/ronaldfischer)\
**Post date:** [August 14, 2007, 12:06pm UTC](https://www.ruby-forum.com/t/exchanging-the-extension-in-a-filename/112024/1 "2007-08-14T12:06:22Z")

</div>

I have a string representing the name of a java file. I would  
like to get a new string, where the extension .java has been  
replaced by .class.

My approach was to use String#sub:

filename=“abc/x.java”  
filename.sub(/java$/,‘class’)

This works fine, but I wonder whether there isn’t a more  
elegant way to do it.

Ronald

---

<div class="post-metadata">

**Author:** ![ronaldfischer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/67e7ee/32.png) [@ronaldfischer](https://www.ruby-forum.com/u/ronaldfischer)\
**Post date:** [August 14, 2007, 12:28pm UTC](https://www.ruby-forum.com/t/exchanging-the-extension-in-a-filename/112024/2 "2007-08-14T12:28:25Z")

</div>

On 8/14/07, Ronald F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> This works fine, but I wonder whether there isn’t a more  
> elegant way to do it.

sub is pretty elegant.  
If you wanted you could do it like File.join(File.dirname(filename),  
File.basename(filename, “.java”) + “.class”)

Ronald

---

<div class="post-metadata">

**Author:** ![ronaldfischer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/67e7ee/32.png) [@ronaldfischer](https://www.ruby-forum.com/u/ronaldfischer)\
**Post date:** [August 14, 2007, 5:07pm UTC](https://www.ruby-forum.com/t/exchanging-the-extension-in-a-filename/112024/3 "2007-08-14T17:07:28Z")

</div>

- Ronald F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) (12:05) schrieb:

> filename.sub(/java$/,‘class’)

I’d match the point too, not everything that ends with “java” is java  
source file: filename.sub(/.java$/, ‘.class’)

> This works fine, but I wonder whether there isn’t a more  
> elegant way to do it.

What is unelegant about that?

mfg, simon … l

---

<div class="post-metadata">

**Author:** ![ronaldfischer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/67e7ee/32.png) [@ronaldfischer](https://www.ruby-forum.com/u/ronaldfischer)\
**Post date:** [August 14, 2007, 7:56pm UTC](https://www.ruby-forum.com/t/exchanging-the-extension-in-a-filename/112024/4 "2007-08-14T19:56:47Z")

</div>

On Aug 14, 5:05 am, “Ronald F.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> elegant way to do it.
> 
> ## Ronald
> 
> Ronald F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> Phone: +49-89-452133-162

filename = “abc/x.java”  
==\>“abc/x.java”  
filename[/java$/] = “class”  
==\>“class”  
filename  
==\>“abc/x.class”

---

<div class="post-metadata">

**Author:** ![ronaldfischer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/67e7ee/32.png) [@ronaldfischer](https://www.ruby-forum.com/u/ronaldfischer)\
**Post date:** [August 16, 2007, 9:55am UTC](https://www.ruby-forum.com/t/exchanging-the-extension-in-a-filename/112024/5 "2007-08-16T09:55:24Z")

</div>

> filename = “abc/x.java”  
> ==\>“abc/x.java”  
> filename[/java$/] = “class”  
> ==\>“class”  
> filename  
> ==\>“abc/x.class”

Thanks - this is a nice alternative (for the case I actually  
want to modify filename). Of course it assumes that I know  
that the file ends in .java (but this is true in my case anyway).

Ronald

---

<div class="post-metadata">

**Author:** ![ronaldfischer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/67e7ee/32.png) [@ronaldfischer](https://www.ruby-forum.com/u/ronaldfischer)\
**Post date:** [August 16, 2007, 9:59am UTC](https://www.ruby-forum.com/t/exchanging-the-extension-in-a-filename/112024/6 "2007-08-16T09:59:24Z")

</div>

> - Ronald F. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) (12:05) schrieb:
> 
> > filename.sub(/java$/,‘class’)
> 
> I’d match the point too, not everything that ends with “java” is java  
> source file: filename.sub(/.java$/, ‘.class’)

In general, yes, but in my case I _know_ that the file ends in .java  
anyway, so matching the dot is not so important.

> > This works fine, but I wonder whether there isn’t a more  
> > elegant way to do it.
> 
> What is unelegant about that?

Maybe I did too much shell programming before 😉

On shell level, I would do a

$(dirname $FILENAME)/($basename $FILENAME java)class

so I was thinking whether I might have overlooked some useful method  
in the FilePath or File class which would be helpful here.

Thanks for the advice…

Ronald

---

<div class="post-metadata">

**Author:** ![ronaldfischer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/67e7ee/32.png) [@ronaldfischer](https://www.ruby-forum.com/u/ronaldfischer)\
**Post date:** [August 16, 2007, 10:45am UTC](https://www.ruby-forum.com/t/exchanging-the-extension-in-a-filename/112024/7 "2007-08-16T10:45:33Z")

</div>

On Aug 16, 12:57 am, “Ronald F.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> > > This works fine, but I wonder whether there isn’t a more  
> > > so I was thinking whether I might have overlooked some useful method  
> > > in the FilePath or File class which would be helpful here.
> 
> Thanks for the advice…
> 
> Ronald

If what you are looking for is a way to change the extension of some  
".java’ files  
to have the extension “.class”, you might try Rio (http://  
[rio.rubyforge.org](http://rio.rubyforge.org))

require ‘rio’  
rio(‘abc’).rename files(‘\*.java’) do |file|  
file.extname = ‘.class’  
end

If you want to include files in subdirectories of ‘abc’ use:

rio(‘abc’).rename.all.files(‘\*.java’) do |file|  
file.extname = ‘.class’  
end

Hope this helps,

> **[require 'rio'](http://rio4ruby.blogspot.com/)**
>
> Discussion of the Rio library for Ruby

---

<div class="post-metadata">

**Author:** ![ronaldfischer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/67e7ee/32.png) [@ronaldfischer](https://www.ruby-forum.com/u/ronaldfischer)\
**Post date:** [August 16, 2007, 7:10pm UTC](https://www.ruby-forum.com/t/exchanging-the-extension-in-a-filename/112024/8 "2007-08-16T19:10:13Z")

</div>

Ronald F. wrote:

> I have a string representing the name of a java file. I would  
> like to get a new string, where the extension .java has been  
> replaced by .class.

[…]

> This works fine, but I wonder whether there isn’t a more  
> elegant way to do it.

If you are _really_ lazy:

require ‘rake’ # NOTE: Not running rake, just using its library.

java\_file = “filename.java”  
class\_file = java\_file.ext(“class”)

If you have a bunch of files that need this, you can do:

require ‘rake’

java\_files = FileList[‘src/\*\*/\*.java’]  
class\_files = java\_files.ext(“class”)

If you need more extensive filename manipulation, check out the  
“pathmap” method (available on both FileLists and Strings when using  
rake).

–  
– Jim W.

---

<div class="post-metadata">

**Author:** ![ronaldfischer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/67e7ee/32.png) [@ronaldfischer](https://www.ruby-forum.com/u/ronaldfischer)\
**Post date:** [August 16, 2007, 4:22pm UTC](https://www.ruby-forum.com/t/exchanging-the-extension-in-a-filename/112024/9 "2007-08-16T16:22:14Z")

</div>

2007/8/14, William J. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> > 
> 
> filename[/java$/] = “class”  
> ==\>“class”  
> filename  
> ==\>“abc/x.class”

I’d rather include the dot to avoid renaming files called “foo.notjava”

irb(main):001:0\> f=“foo/bar.java”  
=\> “foo/bar.java”  
irb(main):002:0\> f[/.java$/]=“.class”  
=\> “.class”  
irb(main):003:0\> f  
=\> “foo/bar.class”

Kind regards

robert
