I'll start with an example, as it's easier to explain what I need.
Say for example I've a Project folder called "MainFolder". In this folder there are a number of subfolders and nested subfolders. One of these folders is called e.g. "Data", and there is one file in it called "data1.m" In another folder (say "Script") there is a notebook ("script.nb") that imports the file "data1.m".
I'd like to write a code that will be part of the notebook "script.nb" that finds and imports the file "data1.m" wherever the folder Script and Data are.
This is because I may want to move the subfolders Script and Data in other folders without having to change the code.
The assumptions are: - the folder name "MainFolder" is fixed - all the project folders are in "MainFolder" - the folder name "Data"is fixed - the file name "data1.m" is fixed - there may be other "data1.m" files in other subfolders of the project, but in such a case the subfolders won't be called "Data" - the code must work on different machines with different OSs (Windows, macOS and Linux)
Here an example scheme of the folder structure, where I want to access "data1.m" wherever it is.
I do have an idea of how to do that, but it seems to me that it is quite un-elegant and a bit verbose. I'll share that anyway despite the fact I don't think it's the best way of doing it:
Select[ FileNames["Data", FileNameJoin@#[[;; Position[#, "MainFolder"][[-1, 1]]]] &@ FileNameSplit[NotebookDirectory], Infinity], DirectoryQ][]