# Stripping the extensions off of a file

**URL:** <https://www.ruby-forum.com/t/stripping-the-extensions-off-of-a-file/120357>\
**Category:** Ruby\
**Created:** [November 16, 2007, 4:40am UTC](https://www.ruby-forum.com/t/stripping-the-extensions-off-of-a-file/120357 "2007-11-16T04:40:46Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3bc359/32.png) [@pood](https://www.ruby-forum.com/u/pood)\
**Post date:** [November 16, 2007, 4:40am UTC](https://www.ruby-forum.com/t/stripping-the-extensions-off-of-a-file/120357/1 "2007-11-16T04:40:46Z")

</div>

I’m building a program that goes thru a file structure, and it will find  
the longest filename and biggest files.

Find.find(directory) do |f|  
file\_sizes[File.size(f)]= f  
if File.file?(f)  
file\_stripper = File.basename(f).delete(".jpg") #FIGURE OUT TO  
STRIP THE ExTENSION

```
     file_length[f] = file_stripper
 end

```

end

I got that part, but I can’t seem to be able to strip the extension,  
because I don’t want to count the extensions part part of the length.

Can the delete method take regexep? WHat would the regexep for something  
like anything after the “.”?

thanks!

---

<div class="post-metadata">

**Author:** ![pood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3bc359/32.png) [@pood](https://www.ruby-forum.com/u/pood)\
**Post date:** [November 16, 2007, 5:18am UTC](https://www.ruby-forum.com/t/stripping-the-extensions-off-of-a-file/120357/2 "2007-11-16T05:18:30Z")

</div>

From: Feng T. [[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)]

# Find.find(directory) do |f|

# file\_sizes[File.size(f)]= f

# if File.file?(f)

# file\_stripper = File.basename(f).delete(“.jpg”)

# #FIGURE OUT TO

# STRIP THE ExTENSION

get the extension, then pass it to #basename

## botp@pc4all:~$ qri File.basename --------------------------------------------------------- File::basename File.basename(file\_name [, suffix] ) → base\_name

```
 Returns the last component of the filename given in file_name,
 which must be formed using forward slashes (``/'') regardless of
 the separator used on the local file system. If suffix is given
 and present at the end of file_name, it is removed.

    File.basename("/home/gumby/work/ruby.rb") #=> "ruby.rb"
    File.basename("/home/gumby/work/ruby.rb", ".rb") #=> "ruby"

```

:~$ irb  
~001:0\> name=“/home/botp/test.rb”  
=\> “/home/botp/test.rb”  
~002:0\> File.basename(name,File.extname(name))  
=\> “test”

kind regards -botp

---

<div class="post-metadata">

**Author:** ![pood](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/3bc359/32.png) [@pood](https://www.ruby-forum.com/u/pood)\
**Post date:** [November 16, 2007, 5:45am UTC](https://www.ruby-forum.com/t/stripping-the-extensions-off-of-a-file/120357/3 "2007-11-16T05:45:24Z")

</div>

great thanks! I didn’t know the File library had that method.
