mko
mko

Reputation: 22064

How do I get all the files names in one folder using Ruby?

These are in a folder:

This_is_a_very_good_movie-y08iPnx_ktA.mp4
myMovie2-lKESbDzUwUg.mp4
his_is_another_movie-lKESbDzUwUg.mp4

How do I fetch the first part of the string mymovie1 from the file by giving the last part, y08iPnx_ktA? Something like:

get_first_part("y08iPnx_kTA") #=> "This_is_a_very_good_movie"

Upvotes: 0

Views: 534

Answers (4)

the Tin Man
the Tin Man

Reputation: 160551

If the filenames only have a single hyphen:

path = '/Users/greg/Desktop/test'
target = 'rb'

def get_files(path, target)
  Dir.chdir(path) do
    return Dir["*#{ target }*"].map{ |f| f.split('-').first }
  end
end

puts get_files(path, 'y08iPnx_ktA')
# >> This_is_a_very_good_movie

If there are multiple hyphens:

def get_files(path, target)
  Dir.chdir(path) do
    return Dir["*#{ target }*"].map{ |f| f.split(target).first.chop }
  end
end

puts get_files(path, 'y08iPnx_ktA')
# >> This_is_a_very_good_movie

If the code is assumed to be running from inside the directory containing the files, then Dir.chdir can be removed, simplifying things to either:

puts Dir["*#{ target }*"].map{ |f| f.split('-').first }
# >> This_is_a_very_good_movie

or

puts Dir["*#{ target }*"].map{ |f| f.split(target).first.chop }
# >> This_is_a_very_good_movie

Upvotes: 1

Steve Wilhelm
Steve Wilhelm

Reputation: 6260

Can it be done cleaner than this? REVISED based on @Tin Man's suggestion

def get_first_part(path, suffix)
  Dir.glob(path + "*" + suffix + "*").map { |x| File.basename(x).gsub(Regexp.new("#{suffix}\.*$"),'') } 
end

puts get_first_part("/path/to/files/", "-y08iPnx_kTA")

Upvotes: 1

Lee Jarvis
Lee Jarvis

Reputation: 16241

def get_first_part(path, suffix)
  Dir.entries(path).find do |fname|
    File.basename(fname, File.extname(fname)).end_with?(suffix)
  end.split(suffix).first
end

Kind of expands on the answer from @Steve Wilhelm -- except doesn't use glob (there's no need for it when we're only working with filenames), avoids Regexp and uses File.exname(fname) to the File.basename call so you don't have to include the file extension. Also returns the string "This_is_a_very_good_movie" instead of an array of files.

This will of course raise if no file could be found.. in which case if you just want to return nil if a match couldn't be found:

def get_first_part(path, suffix)
  file = Dir.entries(path).find do |fname|
    File.basename(fname, File.extname(fname)).end_with?(suffix)
  end
  file.split(suffix).first if file
end

Upvotes: 1

user166390
user166390

Reputation:

Break the problem into into parts. The method get_first_part should go something like:

  1. Use Dir to get a listing of files.

  2. Iterate over each file and;

    1. Extract the "name" ('This_is_a_very_good_movie') and the "tag" ('y08iPnx_ktA'). The same regex should be used for each file.

    2. If the "tag" matches what is being looked for, return "name".

Happy coding.


Play around in the REPL and have fun :-)

Upvotes: 3

Related Questions