1
$\begingroup$

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):

samefolder/A01/122/AB
samefolder/A02/123/A
samefolder/A03/124/AB
samefolder/A04/125/AB
samefolder/A05/126/A

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:

SetDirectory["samefolder"]
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 :)

$\endgroup$
1
$\begingroup$

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

 SetDirectory[NotebookDirectory[]];

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

(*{"A01\\122", "A03\\124", "A04\\125"}*)
$\endgroup$
  • 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.