# Problem using wildcards in TChain-\>Add

**URL:** <https://root-forum.cern.ch/t/problem-using-wildcards-in-tchain-add/31474>\
**Category:** ROOT\
**Created:** [November 6, 2018, 2:32pm UTC](https://root-forum.cern.ch/t/problem-using-wildcards-in-tchain-add/31474 "2018-11-06T14:32:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![zoso](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/z/bcef8e/32.png) [@zoso](https://root-forum.cern.ch/u/zoso)\
**Post date:** [November 6, 2018, 2:32pm UTC](https://root-forum.cern.ch/t/problem-using-wildcards-in-tchain-add/31474/1 "2018-11-06T14:32:50Z")

</div>

* * *

\_ROOT Version: root 6.14.04-x86\_64-slc6-gcc62-opt  
_Platform:_ Not Provided  
_Compiler:_ Not Provided

* * *

Hi,

I got stuck using wildcards to add files in a TChain. When I tried to add all the root files stored in a directory (“main\_dir”) I used:

```auto
chain->Add("path/to/main_dir/*");

```

and worked fine. But now, the files are in different subdirectories inside the main directory:

main\_dir/subdir\_1/\*  
main\_dir/subdir\_2/\*  
main\_dir/subdir\_3/\*  
…

I tried with:

```auto
chain->Add("path/to/main_directory/*/*");

```

but it didn’t work. I thinks it’s realted with the regex, but I couldn’t find a solution, and maybe you can help me.

Thanks in advance,  
Gonzalo

---

<div class="post-metadata">

**Author:** ![behrenhoff](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/behrenhoff/32/12285_2.png) [@behrenhoff](https://root-forum.cern.ch/u/behrenhoff)\
**Post date:** [November 6, 2018, 6:42pm UTC](https://root-forum.cern.ch/t/problem-using-wildcards-in-tchain-add/31474/2 "2018-11-06T18:42:31Z")

</div>

The TChain::Add function doesn’t do globbing, but loops over all files in the specified directory and tries to match them. So no wildcards allowed for directories (wildcards are not even allowed for all types of filesystems). Documentation says _Wildcard treatment is triggered by the any of the special characters []\*? which may be used in the file name_  
So you need to loop all by yourself.

With ROOT:

```auto
auto dir = gSystem->OpenDirectory("main_dir");
while (auto f = gSystem->GetDirEntry(dir)) { 
  if (!strcmp(file,".") || !strcmp(file,"..")) continue;
  chain->Add(TString("main_dir/") + f + "/*.root");
}
gSystem->FreeDirectory(dir);

```

Or you can use globbing:

```auto
#include <glob.h>
std::vector<std::string> glob(const char *pattern) {
    glob_t g;
    glob(pattern, GLOB_TILDE, nullptr, &g); // one should ensure glob returns 0!
    std::vector<std::string> filelist;
    filelist.reserve(g.gl_pathc);
    for (size_t i = 0; i < g.gl_pathc; ++i) {
        filelist.emplace_back(g.gl_pathv[i]);
    }
    globfree(&g);
    return filelist;
}

for (const auto &filename : glob("path/to/main_directory/*/*.root")) {
    chain->Add(filename.c_str());
}

```

---

<div class="post-metadata">

**Author:** ![zoso](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/z/bcef8e/32.png) [@zoso](https://root-forum.cern.ch/u/zoso)\
**Post date:** [November 6, 2018, 7:48pm UTC](https://root-forum.cern.ch/t/problem-using-wildcards-in-tchain-add/31474/3 "2018-11-06T19:48:31Z")

</div>

It worked perfectly.

Thank you very much!

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [November 20, 2018, 7:57pm UTC](https://root-forum.cern.ch/t/problem-using-wildcards-in-tchain-add/31474/4 "2018-11-20T19:57:08Z")

</div>

Closed on November 20, 2018, 7:57pm UTC

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.

---

<div class="post-metadata">

**Author:** ![jonas](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/jonas/32/24756_2.png) [@jonas](https://root-forum.cern.ch/u/jonas)\
**Post date:** [December 11, 2023, 5:00pm UTC](https://root-forum.cern.ch/t/problem-using-wildcards-in-tchain-add/31474/5 "2023-12-11T17:00:02Z")

</div>

See also this forum post, which links to a Jira issue that links to the PR that now implements this feature in ROOT itself:

> [@TChain and wildcard](https://root-forum.cern.ch/t/tchain-and-wildcard/36369/4):
>
> This is a good idea. I opened [https://sft.its.cern.ch/jira/browse/ROOT-10371](https://sft.its.cern.ch/jira/browse/ROOT-10371) to keep track of the progress. Would you be interested in proposing a patch/implementation?
