Search code examples
qtcmdqt5shellexecute

How to execute .cmd fille in Qt5


How to execute .cmd file with this commands:

TASKKILL /F /PID 14364
MOVE /Y "C:/Users/BBCCA/AppData/Roaming/DWAKU2\DWAKU2.exe" "D:/DWAKU2/build-    DWAKU2-Desktop_Qt_5_15_2_MinGW_64_bit_Static-Release/release"
START "" "D:/DWAKU2/build-DWAKU2-Desktop_Qt_5_15_2_MinGW_64_bit_Static-Release/release/DWAKU2.exe"

How can I execute this script with command console in qt?
As you can see - first command close qt application.


Firstly, I tried to use QProcess to execute all commands inside it.

auto programmName = QFileInfo(QCoreApplication::applicationFilePath()).fileName();
QProcess consola;
QString command = "cmd";
QStringList commandArgs;
QString subcommand1, subcommand2;
subcommand1 += "TASKKILL /IM ";
subcommand1 += programmName;
subcommand2 += "DEL /Q ";
subcommand2 += QCoreApplication::applicationFilePath();
commandArgs << "/c" << subcommand1 << "&&" << subcommand2;
qDebug() << commandArgs;
consola.startDetached(command, commandArgs);
consola.waitForFinished();

But it doesn't execute with error:

Ошибка: Неправильный параметр или аргумент - '/Q'. Введите "TASKKILL /?" для получения справки по использованию.

Translation:

Error: Incorrect parameter or argument - '/Q'. Enter "TASKKILL /?" for help on how to use it.

After lots of reading docs and asking ru.stuckoverflow - gave up on this idea and tried to use cmd file.
You can read it above.

Execution of this file was:

// Make updater script
    if (fileCorrect){
        ui->LoadingAnimationLabel->setText(bLText + "Making updater script... " + aLText);
        QFile file(appDataPath + QDir::separator() + "updater.cmd");
        if (file.open(QIODevice::WriteOnly)) {

            auto pid = "TASKKILL /F /PID " + QString::number(QCoreApplication::applicationPid()) + "\r\n";
            auto move = "MOVE /Y \"" + appDataPath + QDir::separator() + QFileInfo(QCoreApplication::applicationFilePath()).fileName() + "\" \"" + QCoreApplication::applicationDirPath() + "\"\r\n";
            auto start = "START \"\" \"" + QCoreApplication::applicationFilePath() + "\"\r\n";

        file.write(pid.toLocal8Bit());
        file.write(move.toLocal8Bit());
        file.write(start.toLocal8Bit());

        } else fileCorrect = false;
        file.close();
    }

    // Installing updates
    if (fileCorrect){
        ui->LoadingAnimationLabel->setText(bLText + "Installing update files... " + aLText);
        auto command = appDataPath + QDir::separator() + "updater.cmd";
        QProcess updater;
        updater.startDetached(appDataPath + QDir::separator() + "updater.cmd");
    }

When programm executed this file - only first command was executed.
But when I tried to execute this file by clicking it - it worked fine....


Solution

  • Take a look at QProcess, and particularly the startDetached method. According to the docs, "If the calling process exits, the detached process will continue to run unaffected."

        QProcess *myProcess = new QProcess();
        myProcess->setProgram("updater.cmd");
        myProcess->startDetached();