# Looping over folders and opening each folder to access the root file

**URL:** <https://root-forum.cern.ch/t/looping-over-folders-and-opening-each-folder-to-access-the-root-file/39774>\
**Category:** Newbie\
**Created:** [June 5, 2020, 8:46am UTC](https://root-forum.cern.ch/t/looping-over-folders-and-opening-each-folder-to-access-the-root-file/39774 "2020-06-05T08:46:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![sur](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/c57346/32.png) [@sur](https://root-forum.cern.ch/u/sur)\
**Post date:** [June 5, 2020, 8:46am UTC](https://root-forum.cern.ch/t/looping-over-folders-and-opening-each-folder-to-access-the-root-file/39774/1 "2020-06-05T08:46:24Z")

</div>

Hi all,  
I am new to root and wanted to ask basic question of accessing the root files. I would like to loop over the folders and then opening each folder and then open the root file inside these folders. Also, I am using root version 6.2.

So I am using TSystemDirectory for looping over the folders

```auto
TSystemDirectory dir("MyDir", "/path");
  auto files = dir.GetListOfFiles();
  for (auto fileObj : *files) {
    auto file = (TSystemFile*) fileObj;
}

```

Thank you very much.

---

<div class="post-metadata">

**Author:** ![RongkunWang](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/rongkunwang/32/36036_2.png) [@RongkunWang](https://root-forum.cern.ch/u/RongkunWang)\
**Post date:** [June 5, 2020, 10:47am UTC](https://root-forum.cern.ch/t/looping-over-folders-and-opening-each-folder-to-access-the-root-file/39774/2 "2020-06-05T10:47:32Z")

</div>

> <https://stackoverflow.com/questions/612097/how-can-i-get-the-list-of-files-in-a-directory-using-c-or-c>

inside the loop you can do `f = TFile(*iter)`

RK

---

<div class="post-metadata">

**Author:** ![sur](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/s/c57346/32.png) [@sur](https://root-forum.cern.ch/u/sur)\
**Post date:** [June 10, 2020, 8:33am UTC](https://root-forum.cern.ch/t/looping-over-folders-and-opening-each-folder-to-access-the-root-file/39774/3 "2020-06-10T08:33:50Z")

</div>

Dear RK,

I can’t able to get your comment. Can you explain little more?  
I can already get the list of files from the above code but I want to open each of these folders and get the root file.
