Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Exchanging the extension in a filename

Reply
Thread Tools

Exchanging the extension in a filename

 
 
Ronald Fischer
Guest
Posts: n/a
 
      08-14-2007
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=3D"abc/x.java"
filename.sub(/java$/,'class')=20

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

Ronald
--=20
Ronald Fischer <(E-Mail Removed)>
Phone: +49-89-452133-162

 
Reply With Quote
 
 
 
 
Simon Krahnke
Guest
Posts: n/a
 
      08-14-2007
* Ronald Fischer <(E-Mail Removed)> (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
 
Reply With Quote
 
 
 
 
William James
Guest
Posts: n/a
 
      08-14-2007
On Aug 14, 5:05 am, "Ronald Fischer" <(E-Mail Removed)>
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.
>
> 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
> --
> Ronald Fischer <(E-Mail Removed)>
> Phone: +49-89-452133-162


filename = "abc/x.java"
==>"abc/x.java"
filename[ /java$/ ] = "class"
==>"class"
filename
==>"abc/x.class"

 
Reply With Quote
 
Ronald Fischer
Guest
Posts: n/a
 
      08-16-2007
> > filename=3D"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
> > --
> > Ronald Fischer <(E-Mail Removed)>
> > Phone: +49-89-452133-162

>=20
> filename =3D "abc/x.java"
> =3D=3D>"abc/x.java"
> filename[ /java$/ ] =3D "class"
> =3D=3D>"class"
> filename
> =3D=3D>"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
=20

 
Reply With Quote
 
Ronald Fischer
Guest
Posts: n/a
 
      08-16-2007
> * Ronald Fischer <(E-Mail Removed)> (12:05) schrieb:
>=20
> > filename.sub(/java$/,'class')=20

>=20
> I'd match the point too, not everything that ends with "java" is java
> source file: filename.sub(/\.java$/, '.class')=20


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.

>=20
> What is unelegant about that?


Maybe I did too much shell programming before

On shell level, I would do a=20

$(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

 
Reply With Quote
 
rio4ruby
Guest
Posts: n/a
 
      08-16-2007
On Aug 16, 12:57 am, "Ronald Fischer" <(E-Mail Removed)>
wrote:
> > * Ronald Fischer <(E-Mail Removed)> (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


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)

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,
http://rio4ruby.blogspot.com/


 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      08-16-2007
2007/8/14, William James <(E-Mail Removed)>:
> On Aug 14, 5:05 am, "Ronald Fischer" <(E-Mail Removed)>
> 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.
> >
> > 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
> > --
> > Ronald Fischer <(E-Mail Removed)>
> > Phone: +49-89-452133-162

>
> filename = "abc/x.java"
> ==>"abc/x.java"
> 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

 
Reply With Quote
 
Jim Weirich
Guest
Posts: n/a
 
      08-16-2007
Ronald Fischer 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 Weirich
--
Posted via http://www.ruby-forum.com/.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
The filename set in the response.setHeader("Content-Disposition", "attachment; filename=test.csv") is being ignored! Ed Java 10 07-13-2010 12:43 PM
Re: filename.gif or filename.gif.jpg? Beauregard T. Shagnasty HTML 1 05-30-2008 01:23 PM
Stitch rar files ( filename.part01 and filename.part02) Please help ixgor Software 1 10-15-2006 02:33 AM
how to get 8.3 format filename from long filename jacobyv@sis.unibe.ch Java 1 06-15-2006 10:39 AM
Extract filename from a filename typed by user =?Utf-8?B?Sm9l?= ASP .Net 1 08-23-2004 11:29 PM



Advertisments