# File basenames using File.basename . .

**URL:** <https://www.ruby-forum.com/t/file-basenames-using-file-basename/93877>\
**Category:** Ruby\
**Created:** [April 12, 2006, 9:25pm UTC](https://www.ruby-forum.com/t/file-basenames-using-file-basename/93877 "2006-04-12T21:25:30Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [April 12, 2006, 9:25pm UTC](https://www.ruby-forum.com/t/file-basenames-using-file-basename/93877/1 "2006-04-12T21:25:30Z")

</div>

I need to truncate the extension of some of my files, so that I can add  
a different extension for a separate write file. I’ve tried this:

txtfiles = Dir.glob("_.txt")  
txtfiles.each { |txtfile| File.basename(“txtfile”, "_.txt") }

which yields this:

=\> [“eula.txt”, “libcurl.COPYING.txt”, “openssl.LICENSE.txt”,  
“psout2.txt”, “run  
dll32.exe.Z-missing.txt”]

Obviously, it didn’t strip the extensions. What am I doing wrong?

Thanks.

---

<div class="post-metadata">

**Author:** ![bodikp](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/e79b87/32.png) [@bodikp](https://www.ruby-forum.com/u/bodikp)\
**Post date:** [April 12, 2006, 10:59pm UTC](https://www.ruby-forum.com/t/file-basenames-using-file-basename/93877/2 "2006-04-12T22:59:16Z")

</div>

don’t think you need the wildcard:

> txtfiles.each { |txtfile| File.basename(“txtfile”, “\*.txt”) }
