TDirectory::mkdir with no error if directory already exists


I wonder whether there is a way to use TDirectory::mkdir as in bash with -p option. That is, no error would be raised if the directory already exists (and pointer to that directory would be returned).

Thanks, Kaspi.

Hi Kaspi,

Support for option “-P” would imply adding an extra parameter to mkdir.
In your case you can easily do

TDirectory *dir = parentdir->Get("mydir"); if (!dir) dir = parentdir->mkdir("mydir","dirtitle");

1 Like


many thanks for your reply!

In my personal opinion, adding the extra parameter to mkdir might be useful and worth sometimes. As an example, I’m now performing an extensive MC study of an algorithm, that produces lots of graphs and histograms. The only way not to get lost is to organize them into a directory structure. Now imagine that you have lots of mkdir calls to be replaced by two lines of code … what I’ll do is to put those two lines in a function like “mkdirMinusP” and call this function instead of mkdir. It would be so convenient if the “mkdirMinusP” function came directly with ROOT.

Thanks again, Kaspi.