Pergunta

Hi I'm trying to iterate through directories and remove files with the extension "~" here is my code

    QString path = "/home/brett/sweetback";
    QDirIterator it(path, QDirIterator::Subdirectories);
    while (it.hasNext()) {
        //ui.textEdit->append(QString(it.next()));
        QFileInfo Info(it.next());
        //ui.textEdit->append(QString(Info.fileName()));
        QString testName = QString(Info.fileName());
        QString subString = testName.right(1);
        if(subString == QString("~")){  
                //wnat to remove file here

            ui.textEdit->append(QString(subString));
            remove(QString( testName));
        }
   }

I can list the file fine but cant figure out how to delete them

Foi útil?

Solução

I think you're looking for QFile::remove()

It's a static member of QFile, so you would use it like this:

QFile::remove(testName);

Outras dicas

bool QFile::remove(const QString & fileName) [static]

This is an overloaded function.

Removes the file specified by the fileName given.

Returns true if successful; otherwise returns false.

So, change your code:

remove(QString( testName));

to:

if (!QFile::remove(testName))
    qDebug() << "Could not remove the file:" << testName;

Note that you do not need to cast a QString to QString explicitly. That is superfluous.

You could also use the non-static member method, and then you could even get the error string by using errorString() for the QFile instance when the deletion is not successful.

If you would also like to delete whole directories recursively having the desired ~ suffix, you would need to use the remove member method in QDir for such cases.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top