Poll a directory for files and perform some action on files

Dear ROOT Devs,

I have an application that processes ROOT files and produces histograms.
I want to create a separate application that do the following.

keep polling on a particular directory to check the availability of histogram files and once it found a new file, display it in a canvas.

I tried to get the directory listing using;

void *thedir = gSystem->OpenDirectory(dir)

but this method works only once for me. Cannot call in a loop.
Could you please help me to figure this out?

I am using ROOT on Windows Vista.

Thank you,

This has nothing to do ROOT development.

To find and open some files in some directory I do something like this:

vector fDataFileNames;
int fNumberOfFilesFound = 0;
string prefix = “partOfFileName”;
string data_dir = “fullDirectoryName”;
char* entry;
while ( ( entry = (char*)gSystem->GetDirEntry(dirp)) != NULL )
Int_t flags = 0;
// #ifndef NDEBUG
// cout << "Entry = " << entry << endl;
// cout << “Looking for = " << prefix.c_str() << endl;
// #endif
if (!fnmatch(prefix.c_str(),entry,flags))
// #ifndef NDEBUG
// cout <<” FOUND "<< endl;
// #endif

                    cout << "directory entry matching prefix : " << entry << endl;

You could use this scheme to periodically check directory for new files don’f forget to wait a little after you find file before you open it to make sure file is completely written.
Of course you’d have to keep list of already opened files to compare to, OR
for example, you could convert vector to map and mark already opened files.

Thank you very much for the explanation.
My problem is actually with the second loop that you have mentioned. The code that you have shown works fine, but I could not get it working inside another loop. The simplest form of what I am trying is the following.

void *thedir = gSystem->OpenDirectory(dir.c_str()); const char *direntry; int currentIndex=0; while(!stop){ while ((direntry = gSystem->GetDirEntry(thedir))) { cout<<direntry<<endl; } gSystem->Sleep(5000); }

Could you please let me know if this is similar to what you have mentioned?

Thank you,

Quite simple and elegant thing to do for you would be to use TTimer class to have your CheckDirectoryAndDrawIfNew() called every so often. It’s interface pretty self explanatory.

Sure, I will have a look.
Thank you very much!