Recursive macro

this macro i’ve done should scan in depth a directory supposed to contain several file of a specific kind (say abcd.Header.efgh.root files where only hte word Header is constant the other may change). The macro scan is just to show how i use the digForFiles. Everything works fine the macro find all the files i want in every subfolder as deep as it is BUT the size of the TList it’s always 1. Do you guess why it never grows?


void headerScan()
TString base = “/home/kusanagi/Data/RootData/Run0001”;
TSystemDirectory *targetDir = new TSystemDirectory("", base);
TList *lnk = digForFiles(targetDir, “Header”);

TList* digForFiles(TSystemDirectory *tsd, string defin){
TList *resList = new TList();
TList *subList = new TList();
TObject *subFile = new TObject();
TList *lnk = tsd->GetListOfFiles();

TSystemFile *file       = (TSystemFile*)lnk->First();
string      *fileDes    = new string(file->GetName());
int loc = 0;

    fileDes = new string(file->GetName());
    if(!((fileDes->compare(".") == 0) || (fileDes->compare("..") == 0))){
        if (file->IsDirectory()){
            subList = digForFiles((TSystemDirectory*)file, defin);
            subFile = subList->First();
                subFile = subList->After(subFile);
        } else {
            loc = fileDes->find(defin.c_str(), 0);
            if (loc != string::npos){
                printf("%d this is the actual size \n", resList->GetSize());
            } else {
    file = (TSystemFile*)lnk->After(file);
return resList;



From what I can see, the behavior is the expected behavior and your script is likely to be working (except for the many memory leaks due to pointless memory allocation and to missing deletes).

The reason what you only see sizes that are 1 is likely to be that there is only one file per directory and that the print statement is located in the part of the code that deal with adding the file in the locally created TList. If you move the print statement to the end of the function (i.e. just before the return statement), you should see an increase in the size of the list (however this increase will be hidden in between many restart (one for each new call to your dig function).

An alternative to the creation and merging of many list is for your to pass an holder list to your function so that it would be something like:

TList *mylist = new TList; digForFiles(mylist,targetDir, "Header");
and the dig function looks like:

void digForFile(TList& output, TSystemDirectory *tsd, string defin){ ... f (file->IsDirectory()){ digForFiles(output, file, defin); .... } else { if (loc != string::npos){ output.AddLast((TObject*)file); } }

Also you should avoid ‘new XXX’ unless you have to have that object really survive the end of the function (i.e you should not have … = new string… in your implementation).
You should also avoid C style case (especially when they are supposed to be uncessary). The only one you need are the one like: