How to loop on folders and subfolders

Dear friends!

I have my files structured in different folders and subfolders more or less like this:

mainfolder/
mainfolder/year1/
mainfolder/year1/data/part1.root
mainfolder/year1/data/part2.root
mainfolder/year1/simu/total_simu.root
mainfolder/year2/data/part1.root
mainfolder/year2/simu/total_simu.root
mainfolder/test.root

My question is: how can I loop over the folder/subfolder structure to get a list of all .root files ?

I tried TSystemDirectory systdir(mainfolder, mainfolder) and with that I can find the year1 and year2 folders, however, I didn’t find an easy way to scan all subfolders.

Thanks
Salva

You can execute:

root[0] .! find . -print | grep "\.root"

that’s one way… may be not the best.