Delete files from a folder with changing content

Attaque

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

My problem is in the repeated check of the folder. on the processing thread. The function is listed in the code below where the problem is that i cannot delete the files from the folder. I'm pretty stuck, so any inputs is 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's 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.

Attaque

The problem turned out to be two problems. One of the problems was that the system was too fast, hereby reading and deleting the files at the same time as the system. I resolved this problem by adding a QTimer which was reset each time a signal from the monitored folder was fired. 500ms from the last change of the system was sufficient. Instead of continuesly reading the files in this folder with QFileSystemWatcher and adding them to a queue, as i originally did, I created a function to mute the system watcher while processing the files in the folder. To compensate the function of the file watcher, i created a recursive loop in the processing thread so that it would keep reading the specified files for as long as there still were files. This could be done in one thread though. You all talk code, so here goes:

Signal and slot setup

// 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();

}

Mute file watcher

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

Processing thread

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();

}

The next thing was to go to go to the preferences of the printer i was printing to, in order to prevent this printer from adding spool files to the folder. You can enable this by clicking "print directly to printer". This solved most of my problems and i was able to delete a web of delays that i had created in order to prevet the program from reading the files produced by the printer.

Hope this helps someone!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How do I delete all files of a particular type from a folder

From Dev

Access files stored in a web content folder from an EJB

From Dev

Delete files from a folder based on another file

From Dev

Windows batch command to delete files from a folder except one file

From Dev

Delete all files in 'folder' or with prefix in Google Cloud Bucket from Java

From Dev

How to delete files with a certain extension from a folder in Andorid

From Dev

Read and delete files from folder

From Dev

delete a folder and its content

From Dev

bash - Pick files from folder, process, delete

From Dev

Listing folder content from google drive, returns removed files too

From Dev

Delete all files from a folder and its sub folders

From Dev

Delete files and folder with exceptions

From Dev

Find and delete folder but not content

From Dev

Delete image from folder

From Dev

Delete zero sized files from a specified folder using batch file

From Dev

How to delete the "_files" folder?

From Dev

Can I delete or move mail files from maildir cur folder?

From Dev

Batch file to delete files from wildcard folder

From Dev

Delete a folder from Github

From Dev

Windows - How do I delete corrupted files from a folder?

From Dev

How to delete files from a folder using a list of file names in windows?

From Dev

Files from folder with ignored content is deleted when pushing to Herku

From Dev

How to delete files with a certain extension from a folder in Andorid

From Dev

What is the best way to delete automatically generated files from a folder, knowing that the folder may include manually created files not to delete?

From Dev

Copy all files from a folder it's content into a document

From Dev

Delete all zip files from a folder recursivley

From Dev

Find and get content from .txt files in a specific folder, Android Studio

From Dev

Anyway delete unique files with a .bat file, using another folder's content as reference?

From Dev

Is it possible to delete all content of a folder?

Related Related

  1. 1

    How do I delete all files of a particular type from a folder

  2. 2

    Access files stored in a web content folder from an EJB

  3. 3

    Delete files from a folder based on another file

  4. 4

    Windows batch command to delete files from a folder except one file

  5. 5

    Delete all files in 'folder' or with prefix in Google Cloud Bucket from Java

  6. 6

    How to delete files with a certain extension from a folder in Andorid

  7. 7

    Read and delete files from folder

  8. 8

    delete a folder and its content

  9. 9

    bash - Pick files from folder, process, delete

  10. 10

    Listing folder content from google drive, returns removed files too

  11. 11

    Delete all files from a folder and its sub folders

  12. 12

    Delete files and folder with exceptions

  13. 13

    Find and delete folder but not content

  14. 14

    Delete image from folder

  15. 15

    Delete zero sized files from a specified folder using batch file

  16. 16

    How to delete the "_files" folder?

  17. 17

    Can I delete or move mail files from maildir cur folder?

  18. 18

    Batch file to delete files from wildcard folder

  19. 19

    Delete a folder from Github

  20. 20

    Windows - How do I delete corrupted files from a folder?

  21. 21

    How to delete files from a folder using a list of file names in windows?

  22. 22

    Files from folder with ignored content is deleted when pushing to Herku

  23. 23

    How to delete files with a certain extension from a folder in Andorid

  24. 24

    What is the best way to delete automatically generated files from a folder, knowing that the folder may include manually created files not to delete?

  25. 25

    Copy all files from a folder it's content into a document

  26. 26

    Delete all zip files from a folder recursivley

  27. 27

    Find and get content from .txt files in a specific folder, Android Studio

  28. 28

    Anyway delete unique files with a .bat file, using another folder's content as reference?

  29. 29

    Is it possible to delete all content of a folder?

HotTag

Archive