Delete files from a folder with content changes

I have a main thread and a thread processing some files. When there are changes in the folder controlled by the main thread, the signal is sent to the processing thread to start. After processing the file, I would like to delete it, and then let the folder check if there are more files in the folder. If there is, repeat the process.

My problem is re-checking the folder. on the processing stream. The function is indicated in the code below, where the problem is that I cannot delete files from the folder. I'm pretty stuck, so any inputs are appreciated.

In dataprocessor.h

...
QList<QString> justProcessed;
...

In dataprocessor.cpp

void DataProcessor::onSignal() {
// Will keep running as long as there are files in the spool folder, eating it way through
bool stillFiles = true;

QDir dir(this->monitoredPath);
dir.setFilter(QDir::Files | QDir::NoDotAndDotDot);
dir.setSorting(QDir::Time);

while(stillFiles) {

    // Have to update on each iteration, since the folder changes.
    dir.refresh();

    QFileInfoList fileList = dir.entryInfoList();
    QString activeFile = "";

    foreach(QFileInfo file, fileList) {

        if((file.suffix() == "txt") && !justProcessed.contains(file.fileName())) {
            // Is a text file. Set for processing and break foreach loop
            activeFile = file.fileName();
            break;
        }

    }

    // If none of the files passed the requirements, then there are no more spl files in the folder.
    qDebug() << activeFile;
    if(activeFile == "") {
        qDebug() << "Finished";
        emit finished();
        stillFiles = false;
    }

    // File is a new file, start processing
    qDebug() << "Selected for processing";
    qDebug() << monitoredPath + "/" + activeFile;
    if(!dir.remove(monitoredPath + "/" + activeFile)) qDebug() << "Could not remove file";

    justProcessed.append(activeFile);

} // While end
}

Please let me know if I missed to provide some information.

+4
1

. , , . , QTimer, reset , . 500 . , QFileSystemWatcher , , . , , , . . , :

// Setting up folder monitoring.
watcher.addPath(worker->monitoredPath);
QObject::connect(&watcher, SIGNAL(directoryChanged(QString)), this,
SLOT(updateQueue()));
// Timer
connect(timer, SIGNAL(timeout()), this, SLOT(startProcess()));
connect(this, SIGNAL(processRequest()), thread, SLOT(start()));
...

void MainWindow::updateQueue() {
  // Starts or restarts the call to start process. Prevents multiple signals from
  // many files added at once
  timer->start(500);
}
...
void MainWindow::startProcess() {

  if(!thread->isRunning()) {
    emit processRequest();
    muteWatcher(true);  // From here on a recursive loop in dataprocessor checks
                        // the folder for new files.
  }
  timer->stop();

}

void MainWindow::muteWatcher(bool toggle) {
  if(toggle) {
    watcher.removePath(worker->monitoredPath);
  } else {
    watcher.addPath(worker->monitoredPath);
  }
}

void DataProcessor::initialize() {
  QDir dir(this->monitoredPath);
  dir.setFilter(QDir::NoDotAndDotDot | QDir::Files);
  dir.setSorting(QDir::Time);

  QList<QString> dFiles;

  foreach(QFileInfo file, dir.entryInfoList()) {

    if(file.suffix().toLower() == "txt") {
        dFiles.append(file.fileName());
    }
  }

  if(dFiles.count() == 0) { // Base case
    emit muteWatcher(false); // Start monitoring the folder again
    emit finished(); // end the thread
    return true;
  }

  // PROCESSING HERE

  initializeLabel();

}

, , . , " ". , , , , .

, -!

0

Source: https://habr.com/ru/post/1534296/


All Articles