0

Apologies, as I am not into Ruby coding, and I am writing a code to get the folder names of the structures from the file path.

For example, if I have a file in E:/aaa/bbb/ccc/filename.jpg

How can I get the folder names aaa, bbb, and ccc? I know only the parent folder ccc to use File.nakeddir, but for aaa and bbb, how to get them?

Thank you!

rency0722
  • 47
  • 5
  • May be you can check this link [Get parent directory of current directory in Ruby](https://stackoverflow.com/questions/8660732/get-parent-directory-of-current-directory-in-ruby) – Preman Terminal Sep 02 '21 at 09:56
  • Did you try searching for it? https://stackoverflow.com/questions/2206714/can-a-ruby-script-tell-what-directory-it-s-in – Denny Mueller Sep 02 '21 at 10:17
  • There is no rush to select an answer but if at least one answer is useful please select the one you prefer. I realize that at this time there is only one answer. – Cary Swoveland Sep 03 '21 at 04:01

1 Answers1

1

The Pathname class from the Ruby standard library can be used to break down paths into their constituent parts:

require 'pathname'
path = Pathname.new("E:/aaa/bbb/ccc/filename.jpg")
path.dirname.each_filename.drop(1) # ["aaa", "bbb", "ccc"]
max
  • 96,212
  • 14
  • 104
  • 165