I hope this question is not a duplicate, but I could not find any other question which answered my specific question, which is:

I have the following folder structure (just an example):


I just want to have the path from the directories where the AB folder is, i.e. A01/122, A03/124, A04/125.

I tried it likes this, but I'm stuck right now:

folders = Select[FileNames[], DirectoryQ[#] &]

which gives me the names of the folder A01 to A05, but that's not really what I want. I only want the names of the folders, where the AB exists (A01/122 and so on).

I appreciate any help on this :)


If you put the MMA file inside "samefolder" directory then:


StringTake[#, {1, -4}] & /@ FileNames["*AB", "", Infinity]

(*{"A01\\122", "A03\\124", "A04\\125"}*)
  • 1
    $\begingroup$ You don't need to put the notebook in the "samefolder" if your write instead : FileNames["AB","absolutePathTosamefolder",Infinity] where "absolutePathTosamefolder" is the absolute path to your "samefolder" $\endgroup$ – SquareOne Feb 5 '15 at 9:55
  • $\begingroup$ Thank you all. The solution of SquareOne worked for me, though I did use a level of 3 rather than Infinity. $\endgroup$ – holistic Feb 5 '15 at 10:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.