0

I have problem with using QProcess class to format pendrive. I checked similar questions, but they didn't help me. How can I enter a few commands to diskpart.exe? It should be something like this?

QProcess process;
process.start("diskpart.exe",QStringList()<<"/C"<<"select disk 1"<<"clean");`

Because it doesn't work, I tried also other solutions, but without result.

QString command("diskpart.exe");
QStringList params=QStringList()<<"/k"<<"select disk 1"<<"clean";
QProcess::startDetached(command,params);

The code above opens diskpart and immediately closes it without cleaning disk. Why is this happening?

I would like to ask about running commands from txt file also, if I write them to txt file:

select disk 1
clean

How to execute this? Because I failed with this too. One more other question, how can I check a letter of pendrive in diskpart in c++? To select a specific pendrive which I select in comboBox in my program?

  • You probably want to use a script file option: http://www.virtues.it/2010/07/howto-diskpart/ – drescherjm Jul 25 '18 at 22:30
  • There are commands to diskpart, I need help how to use it in c++ code with QProcess class – Rander Ens Jul 26 '18 at 10:06
  • 1
    https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/diskpart-scripts-and-examples – bencemeszaros Jul 26 '18 at 11:35
  • Where are you getting the /C and /k command line parameters? I don't see those in a search. The searches say to use a script file option using /s script file. That would be easy to implement using `QProcess`. Use QFile to create a text file (yourFileName.txt) with your commands. then execute diskpart.exe with the following command line parameters: `/s yourfileName.txt` – drescherjm Jul 26 '18 at 15:46
  • It works now with txt script, thanks :) But I need more to write a commands in the code than in txt file. /C and /k command are used with working with cmd etc. But how can I run commands like this: `process.start("diskpart.exe",QStringList()<<"/C"<<"select disk 1"<<"clean");` It doesn't work by this way, other solutions what I tried didn't work also. – Rander Ens Jul 26 '18 at 18:08
  • ***/C and /k command are used with working with cmd etc*** That does not mean every program would support /C or /k. And it certainly does not mean that they would function the same way as `cmd.exe`. – drescherjm Jul 26 '18 at 18:53
  • Just use `process.start("diskpart.exe",QStringList()<<"/s"<<"yourtextfile.txt");` – drescherjm Jul 26 '18 at 18:55
  • I did that and it works, but I need to run commands without txt file. About txt file I asked as a second option, I would like to solve it without txt file. I read about /C and /k command that they serve to choose if program like cmd.exe should works in background or normally. – Rander Ens Jul 26 '18 at 19:24
  • Again `/C` and `/k` will not work for `diskpart.exe`. Every program has its own command line support. There is no reason `diskpart.exe` would support the same options as `cmd.exe`. They are 2 different programs. – drescherjm Jul 26 '18 at 19:48
  • My suggestion is use `QTemporaryFile` to generate a temporary file. http://doc.qt.io/qt-5/qtemporaryfile.html – drescherjm Jul 26 '18 at 19:51
  • Aaa, ok I didn't know this commands don't work on diskpart. QTemporaryFile class looks nice, thanks very much for help :) I need one more information about checking which pendrive on list disk in diskpart corresponds to which letter. Do you know something about it? – Rander Ens Jul 26 '18 at 23:00
  • https://learn.microsoft.com/en-us/windows/desktop/fileio/enumerating-volumes – drescherjm Jul 27 '18 at 15:42
  • Maybe also: https://stackoverflow.com/questions/327718/how-to-list-physical-disks – drescherjm Jul 27 '18 at 15:45

0 Answers0