DashControl
DashControl

Reputation: 284

ruby return section of directory name

I have a directory, which contains a series of folders, which are of the pattern YYYY-MM-DD_NUMBER . If I am navigating through one of these folders using Dir, how can I return part of the folder name that contains YYYY-MM-DD ?

For example, 2013-05-23_160332 would be a name of a folder. And it would be apart of a larger directory, called main_dir. I use Dir to get access to some file names and store them into an array, like so:

array = Dir["/main_dir/**/data/*.csv"]

I then iterate through the array and print the files. How can I also return/print the part of the title directory that I am currently accessing with each iteration (again, in the form of YYYY-MM-DD)?

Upvotes: 0

Views: 72

Answers (1)

squiguy
squiguy

Reputation: 33380

I might do something like this.

re = Regexp.new('\d{4}-\d{2}-\d{2}')

array.each do |folder|
  puts folder[re]
  # folder.each or other processing ...
end

Upvotes: 1

Related Questions