How do I list files in a directory?

Please read tips for efficient and successful posting and posting code

ROOT Version: Not Provided
Platform: Not Provided
Compiler: Not Provided

I’ve written the following code, which is supposed to list all the files in a directory:

const char* inDir = “/path/to/directory/I/want/to/list”;
const char* ext = “.myextension”;

char* dir = gSystem->ExpandPathName(inDir);
void* dirp = gSystem->OpenDirectory(dir);

const char* entry;
const char* filename[100];
Int_t n = 0;
TString str;

while((entry) = (char*)gSystem->GetDirEntry(dirp))){
     str = entry;
          filename[n++] = gSystem->ConcatFileName(dir, entry);

for (Int_t i = 0; i < n; i++){

It works excellent when I set the directory path to %ROOTSYS%/tutorials (the path to the ROOT tutorials) and the extension to “.C”. It lists all the files in the ROOT tutorials. But when I try to list the files in the directory:

None of the files appear. I know they’re there, and I can access the directory via the terminal by using cd /path/to/the/directory and list all the files using .ls. I’ve also tried omitting the part of the code that checks for the extension.

This is the directory of my macro:

What am I doing wrong?

The code looks correct to me. I’m just somewhat confused whether this is on Linux or Windows. The %ROOTSYS% notation suggests Windows, the forward slashes as a directory separator suggest Linux. Anyway, it shouldn’t make a difference, I think Windows accepts forward slashes, too.

I’d suggest to print out entry and str along the way to make sure they are what you expect. You can also try to list the very macro by setting inDir = /disk/home/username/root-codes.


This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.