6

I'm using CMD by QProcess but I have a problem.

My code:

QProcess process;
process.start("cmd.exe");
process.write ("del f:\\b.txt\n\r");
process.waitForFinished();
process.close();

When I don't pass an argument for waitForFinished() it waits for 30 secs. I want to terminate QProcess after CMD command is executed! Not much and not less!

demonplus
  • 5,613
  • 12
  • 49
  • 68

2 Answers2

10

You need to terminate the cmd.exe by sending exit command, otherwise it will wait for commands Here is my suggestion:

QProcess process;
process.start("cmd.exe");
process.write ("del f:\\b.txt\n\r");
process.write ("exit\n\r");
process.waitForFinished();
process.close();
ahmed
  • 5,430
  • 1
  • 20
  • 36
6

The process you're starting is cmd.exe which, by itself will, not terminate. If you call cmd with arguments, you should achieve what you want: -

QProcess process;
process.start("cmd.exe \"del f:\\b.txt"\"");
process.waitForFinished();
process.close();

Note that the arguments are escaped in quotes.

Alternatively, you could call the del process, without cmd: -

QProcess process;
process.start("del \"f:\\b.txt"\"");
process.waitForFinished();
process.close();

Finally, if you just want to delete a file, you could use the QFile::remove function.

QFile file("f:\\b.txt");
if(file.remove())
    qDebug() << "File removed successfully";
TheDarkKnight
  • 27,181
  • 6
  • 55
  • 85