ROOT logo
#include "TFile.h"
#include "TSystem.h"
#include "TMacro.h"
      
void importdir(const char *dirname) {
  //Example of script showing how to create a ROOT file with subdirectories.
  //The script scans a given directory tree and recreates the
  //same structure in the ROOT file.
  //All source files of type .h,cxx,c,dat,py are imported as TMacro objects
  //see also other tutorial readCode.C
  //Author: Rene Brun
         
   char *slash = (char*)strrchr(dirname,'/');
   char *locdir;
   if (slash) locdir = slash+1;
   else       locdir = (char*)dirname;
   printf("processing dir %s\n",dirname);
   TDirectory *savdir = gDirectory;
   TDirectory *adir = savdir->mkdir(locdir);
   adir->cd();
   void *dirp = gSystem->OpenDirectory(dirname);
   if (!dirp) return;
   char *direntry;
   Long_t id, size,flags,modtime;
   //loop on all entries of this directory
   while ((direntry=(char*)gSystem->GetDirEntry(dirp))) {
      TString afile = Form("%s/%s",dirname,direntry);
      gSystem->GetPathInfo(afile,&id,&size,&flags,&modtime);
      if (direntry[0] == '.')             continue; //forget the "." and ".." special cases
      if (!strcmp(direntry,"CVS"))        continue; //forget some special directories
      if (!strcmp(direntry,"htmldoc"))    continue;
      if (strstr(dirname,"root/include")) continue;
      if (strstr(direntry,"G__"))         continue;
      if (strstr(direntry,".c")    ||
          strstr(direntry,".h")    ||
          strstr(direntry,".dat")  ||
          strstr(direntry,".py")   ||
          strstr(direntry,".C")) {
         TMacro *m = new TMacro(afile);
         m->Write(direntry);
         delete m;
      } else {
         if (flags != 3)                     continue; //must be a directory
         //we have found a valid sub-directory. Process it
         importdir(afile);
     }
  }
  gSystem->FreeDirectory(dirp);
  savdir->cd();
}
void importCode() {
   TFile *f = new TFile("code.root","recreate");
   importdir("../../../root"); //change the directory as you like
   delete f;
}
 importCode.C:1
 importCode.C:2
 importCode.C:3
 importCode.C:4
 importCode.C:5
 importCode.C:6
 importCode.C:7
 importCode.C:8
 importCode.C:9
 importCode.C:10
 importCode.C:11
 importCode.C:12
 importCode.C:13
 importCode.C:14
 importCode.C:15
 importCode.C:16
 importCode.C:17
 importCode.C:18
 importCode.C:19
 importCode.C:20
 importCode.C:21
 importCode.C:22
 importCode.C:23
 importCode.C:24
 importCode.C:25
 importCode.C:26
 importCode.C:27
 importCode.C:28
 importCode.C:29
 importCode.C:30
 importCode.C:31
 importCode.C:32
 importCode.C:33
 importCode.C:34
 importCode.C:35
 importCode.C:36
 importCode.C:37
 importCode.C:38
 importCode.C:39
 importCode.C:40
 importCode.C:41
 importCode.C:42
 importCode.C:43
 importCode.C:44
 importCode.C:45
 importCode.C:46
 importCode.C:47
 importCode.C:48
 importCode.C:49
 importCode.C:50
 importCode.C:51
 importCode.C:52
 importCode.C:53
 importCode.C:54
 importCode.C:55
 importCode.C:56