'get relative paths to files in ruby
I have a folder structure as follows:
.
└── FolderA
├── a.cpp
├── b.cpp
├── c.cpp
├── FolderB
│ ├── trial.cpp
│ └── trial1.cpp
└── FolderC
└── srcFolder
└── anothercppfile.cpp
I wanted to store the names of all .cpp files which are present in FolderA in an array. I have to ensure that I preserve the relative paths of the files which are present in folders inside FolderA.
I can use:
require 'find'
cpp_file_paths = []
Find.find('path/to/search') do |path|
cpp_file_paths << path if path =~ /.*\.cpp$/
end
But I do not get the relative paths. I am unsure on how to proceed. The final array must be:
["a.cpp",
"b.cpp",
"c.cpp",
"/FolderB/trial.cpp",
"/FolderB/trial1.cpp",
"/FolderC/srcFolder/anothercppfile.cpp"]
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
