Get and process all files from a directory

Hi there!

I’ve got a sequence of acquisition ASCII .dat files in a directory and I need to write a macro in ROOT that:

  • opens the current working directory
  • gets all .dat file names, possibly with a non a priori fixed number of files
  • writes their names into a non a priori fixed size array of TString (like a c++ vector)
  • for each imported file, opens it and reads its values (columns of data)

I know this is a quite hostile problem, but every hint is really appreciate!
Thanks everybody

luca

Hi luca,

This is quite simple. See for example [url=https://root-forum.cern.ch/t/open-files-in-a-directory-with-a-for-loop/12471/1 post[/url]. And if you still have problems, just let us know.

Cheers, Bertrand.

Hi Bertrand!
Thanks a lot for your hint, it works! But I still want to ask you some clarifications:

  • your script works in interpreted mode, but if I write it in compiled mode including header files,
    TIter.h is not present in my $ROOTSYS/include folder! I compile instead without errors by including
    TIterator.h and even without any specifications of TIter.h … is it the same?

  • I need to read all my .dat files in the right alphabetical order, because I extract my x-values from imported file
    names via TString::First() and TString::Atof()… unfortunately your script imports file in the correct alphabetical
    order running in Windows XP and Vista, but not in my Linux Ubuntu! So my second question: is there any
    ROOT method or option like a bash ls | sort or DOS dir command?

Thanks again!

luca

Hi,

TIter is declared in TCollection.h (including TList.h should solve the problem).
And I always use TIter, never TIterator…

You can try to sort the file list with TList::Sort(Bool_t order = kSortAscending); it should work.

Cheers, Bertrand.