Get name and file extension in Ruby

I am working on a program to download video from YouTube, convert it to MP3 and create a directory structure for files.

My code is:

FileUtils.cd("#{$musicdir}/#{$folder}") do YoutubeDlhelperLibs::Downloader.get($url) if File.exists?('*.mp4') puts 'Remove unneeded tempfile' Dir['*.mp4'].each do |waste| File.delete(waste) end else puts 'Temporary file already deleted' end Dir['*.m4a'].each do |rip| rip.to_s rip.split puts 'Inside the function' puts rip end end 

The first goes to the already created music folder. Inside I execute get . After that, I have two files in the directory: "xyz.mp4" and "xyz.m4a".

I would like to get the file name without the extension so that I can handle both files differently.

I use an array, but the array for one match sounds crazy to me.

Is there another idea?

+58
ruby file
Dec 27 '13 at 0:41
source share
1 answer

You can use the following functions for your purpose:

 file = "/path/to/xyz.mp4" comp = File.basename file # => "xyz.mp4" extn = File.extname file # => ".mp4" name = File.basename file, extn # => "xyz" path = File.dirname file # => "/path/to" 
+148
Dec 27 '13 at 0:52
source share



All Articles