Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ruby / Rails find file with name like

i try to find all files in a directory which has a name like "test".

So in my directory (/test/files/example) i got following files:

  • test_1
  • test_2
  • test_5
  • test_9

How can i get all the files with the File Class in Ruby? I do this but i think you see the struggle

10.times do |count|
  file_path = "/test/files/example/test_#{count}.wav"
  if File.exist?(file_path)
    @files[count] = file_path
    next
  end
  break
end
like image 279
Zero Avatar asked May 20 '15 14:05

Zero


1 Answers

Globbing is the way to go. This will return an array of filenames matching your pattern.

Dir["/test/files/example/test_*.wav"]
like image 106
Sergio Tulentsev Avatar answered Nov 07 '22 23:11

Sergio Tulentsev