2

I need to monitor a directory which contains many files and a process reads and deletes the .txt files from the directory; once all the .txt files are consumed, the consuming process needs to be killed. How do I check if all the .txt files are consumed using C++? I am developing my application on Visual Studio on windows platform.

sand
  • 542
  • 6
  • 16
  • What does "consumed" mean? You cannot detect a process reading a file. – Hans Passant Mar 24 '10 at 17:47
  • 1
    Consumed means it is processed and deleted. – sand Mar 24 '10 at 17:49
  • 1
    Why doesn't the consuming process just exit when there aren't any more files to consume? – Bill Mar 24 '10 at 18:14
  • Consuming process waits for more files and never exits. To test the consuming process, some files are stored in the directory and once they are consumed it needs to be stopped. – sand Mar 25 '10 at 04:53

3 Answers3

5

To get callbacks of when the contents of the directory changed, use the ReadDirectoryChangesW and FindFirstChangeNotification Win32 API.

You can see examples from this question.

Community
  • 1
  • 1
Brian R. Bondy
  • 339,232
  • 124
  • 596
  • 636
2

Use FindFirstChangeNotification to register a notification callback.

Kirill V. Lyadvinsky
  • 97,037
  • 24
  • 136
  • 212
0

Since it was not required to perform action on each txt file deletion. I came up with following code:

{
  intptr_t hFile;
  struct _finddata_t c_file;
  string searchSpec;
  for (size_t i = 0; i < dataPathVec.size(); ++i)
  {
    searchSpec = dataPathVec.at(i) + DIRECTORY_SEPERATOR + "*" + TXT_FILE_EXT;
    hFile = 0;
    while((hFile != -1L) || (ret != 0))
    {
        hFile = _findfirst(searchSpec.c_str(), &c_file);
        Sleep(500);
        if (hFile != -1L)
        {
            ret = _findclose(hFile);
        }
    }
  }
}

It can monitor many folders and wait till all the txt files are deleted from all the monitored folders.

sand
  • 542
  • 6
  • 16