Listing contents of a directory

Hi to everybody !
I have a small question ( maybe questions :slight_smile: :
How can i list the contents of a directory ( lets say someting like >> ls somefile*.root )
the second :
it is posible to do somethin like ls *.root > directory.txt and after that to read that file line by line and make FileLIst->Add for every file read from directory.txt ?
Is an easyer way to do this ?

See class TSystem/TUnixSystem, in particular … nDirectory … etDirEntry

see an example of use in class TSystemDirectory … istOfFiles


Hi! Thank you for your answer but i am not so good at programing so i have more questions .
So … after i do this :

TSystemDirectory *workdir = new TSystemDirectory(“workdir”,gSystem->WorkingDirectory());
TList *RootList = workdir->GetListOfFiles();

i will have a TList which contains all files from curent directory . ( how can i have only a certain type of files ??? )
I seen that TList has a method named First() whis return an TObject
This TObject will be a file from directory ?
can i do something like this :

TObject file_1 = RootList->First();
char file_name = file_1->GetName() ;
Also i seen in TSystemDirectory a method named FindFileObj which return
a TSytemFile . if more than one files are on the list will return the first one ?
also it is posible something like this ? :
TSystemFile *sys_file = workdir->FindFileObj() ;
char file_name_2 = sys_file->GetName() ;
In fact all i want to do is to iterate over a list of files of some type ( lets say .root )
and apply a function on each file . How can i acces the list created to do something like this :
for ( list_index = First() ; list_index=<Last() ; index++ )
file = workdir->list_index ;

Thank you very much for your help

probably slightly off-topic:
and the source file

Hope This Helps. Regards. Valeriy

Hi! Becouse of my poor knowledge of C i dont see where is the code that read directory contents :cry: . Thank you anyway


the following code looks at every entry in a directory and
checks whether it is a directory :

const TString dir = “blablabla”;
void *dirp = gSystem->OpenDirectory(dir);
Char_t *afile;
while ((afile = const_cast<Char_t *>(gSystem->GetDirEntry(dirp)))) {
Long_t id;
Long_t size;
Long_t flags;
Long_t modtime;

const TString path = dir+"/"+afile;
if (gSystem->GetPathInfo(path.Data(),&id,&size,&flags,&modtime))
Int_t isDir = (02 & flags);
if (!isDir)



Thank you very much to you all !!! I did it !!!
Now one more question : From where can i read in detail about gSystem and others gSomething ?