[ruby] How to get filename without extension from file path in Ruby

How can I get the filename from a file path in Ruby?

For example if I have a path of "C:\projects\blah.dll" and I just want the "blah".

Is there a LastIndexOf method in Ruby?

This question is related to ruby

The answer is


Note that double quotes strings escape \'s.

'C:\projects\blah.dll'.split('\\').last

Try File.basename

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"

In your case:

File.basename("C:\\projects\\blah.dll", ".dll")  #=> "blah"

Try this code

Use extname

 File.basename("a/b/d/test.rb", File.extname("a/b/d/test.rb")) #=> "test" 

Jonathon's answer is better, but to let you know somelist[-1] is one of the LastIndexOf notations available.

As krusty.ar mentioned somelist.last apparently is too.

irb(main):003:0* f = 'C:\\path\\file.txt'
irb(main):007:0> f.split('\\')
=> ["C:", "path", "file.txt"]
irb(main):008:0> f.split('\\')[-1]
=> "file.txt"

Try this code

Use extname

 File.basename("a/b/d/test.rb", File.extname("a/b/d/test.rb")) #=> "test" 

You can get directory path to current script with:

File.dirname __FILE__

If you have access to ENV variables, scan combined with this little regex (which finds the last but one word, a dot, then the last word of the string) will put the file's name into 'filename':

filename = ENV['SCRIPT_NAME'].scan(/\w+\.\w+$/)

Obviously, you can use scan and the regex on any path name that includes the filename, and __FILE__ is the obvious choice:

__FILE__.scan(/\w+\.\w+$/)

In case the extension is not known (it needs the / separator):

irb(main):024:0> f = 'C:\foobar\blah.txt'.gsub("\\","/")
=> "C:/foobar/blah.txt"
irb(main):027:0> File.basename(f,File.extname(f))
=> "blah"

You can get directory path to current script with:

File.dirname __FILE__

In case the extension is not known (it needs the / separator):

irb(main):024:0> f = 'C:\foobar\blah.txt'.gsub("\\","/")
=> "C:/foobar/blah.txt"
irb(main):027:0> File.basename(f,File.extname(f))
=> "blah"

Jonathon's answer is better, but to let you know somelist[-1] is one of the LastIndexOf notations available.

As krusty.ar mentioned somelist.last apparently is too.

irb(main):003:0* f = 'C:\\path\\file.txt'
irb(main):007:0> f.split('\\')
=> ["C:", "path", "file.txt"]
irb(main):008:0> f.split('\\')[-1]
=> "file.txt"

Try File.basename

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"

In your case:

File.basename("C:\\projects\\blah.dll", ".dll")  #=> "blah"

Jonathan Lonowski answered perfectly, but there is something that none of the answers mentioned here. Instead of File::extname, you can directly use a '.*' to get the file name.

File.basename("C:\\projects\\blah.dll", ".*") # => "C:\\projects\\blah"

But, if you want to get the base file name of any specific extension files, then you need to use File::extname, otherwise not.


Note that double quotes strings escape \'s.

'C:\projects\blah.dll'.split('\\').last

Jonathon's answer is better, but to let you know somelist[-1] is one of the LastIndexOf notations available.

As krusty.ar mentioned somelist.last apparently is too.

irb(main):003:0* f = 'C:\\path\\file.txt'
irb(main):007:0> f.split('\\')
=> ["C:", "path", "file.txt"]
irb(main):008:0> f.split('\\')[-1]
=> "file.txt"

If you have access to ENV variables, scan combined with this little regex (which finds the last but one word, a dot, then the last word of the string) will put the file's name into 'filename':

filename = ENV['SCRIPT_NAME'].scan(/\w+\.\w+$/)

Obviously, you can use scan and the regex on any path name that includes the filename, and __FILE__ is the obvious choice:

__FILE__.scan(/\w+\.\w+$/)

Note that double quotes strings escape \'s.

'C:\projects\blah.dll'.split('\\').last

Jonathon's answer is better, but to let you know somelist[-1] is one of the LastIndexOf notations available.

As krusty.ar mentioned somelist.last apparently is too.

irb(main):003:0* f = 'C:\\path\\file.txt'
irb(main):007:0> f.split('\\')
=> ["C:", "path", "file.txt"]
irb(main):008:0> f.split('\\')[-1]
=> "file.txt"

Try File.basename

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"

In your case:

File.basename("C:\\projects\\blah.dll", ".dll")  #=> "blah"

Note that double quotes strings escape \'s.

'C:\projects\blah.dll'.split('\\').last

Try File.basename

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"

In your case:

File.basename("C:\\projects\\blah.dll", ".dll")  #=> "blah"

Jonathan Lonowski answered perfectly, but there is something that none of the answers mentioned here. Instead of File::extname, you can directly use a '.*' to get the file name.

File.basename("C:\\projects\\blah.dll", ".*") # => "C:\\projects\\blah"

But, if you want to get the base file name of any specific extension files, then you need to use File::extname, otherwise not.