QProcess and powershell - checking if the disk is clean
-
Hello,
I need help writing the powershell command which will be based on the given disk number, e.g. disk 3 (as in diskpart - sel disk 3) would return the percentage of free space or disk capacity and its occupied space. In fact, my aim is to write a function that will check if the disk is empty. My point is to protect the user from mistakenly wiping the disk and creating a partition on the disk where the files are stored.
Thank you in advance for your help! -
@Piotrek102 What exactly is your question/problem?
What did you try? -
I create windows installation program from wim file. From the application level, the user selects the disk - its index. The function I am creating in c ++ is to use QProcess to run a command (which I need to find) or a ps1 script that will return all indexes of empty disks. Then in c ++ and Qt in my function I will check if any of the indexes returned is equal to the index of the disk selected by the user. If so, the function will return true if not false. Simplifying, the function is to check if the disk selected by the user is empty to avoid formatting the disk with important data.
-
@Piotrek102 You did not answer any of my questions.
Again: what did you try and what is the problem/question? -
I need a command or a powershell script that will return all indexes of the mounted disks in the system and their used space. For example:
disk 0
120GB
disk 1
51GB
... -
@Piotrek102
I don't think this is the right thing to do. But if that is what you want you will need something Windows-specific, if you want a PowerShell command then a PowerShell forum would likely give you better answers than a Qt forum.UPDATE
If the information you require is available from Qt it would be via the QStorageInfo Class. That does have astatic QList<QStorageInfo> mountedVolumes()
, if that gives you the information you seek. -
@Piotrek102 This is not Qt related. If you'd like to know how to do that in Qt (simple) we can assist however PowerShell is outside the subject of this forum.
-
OK, I have a powershell script:
function Get-Space { foreach($disk in Get-CimInstance Win32_Diskdrive) { $diskMetadata = Get-Disk | Where-Object { $_.Number -eq $disk.Index } | Select-Object -First 1 $partitions = Get-CimAssociatedInstance -ResultClassName Win32_DiskPartition -InputObject $disk foreach($partition in $partitions) { $drives = Get-CimAssociatedInstance -ResultClassName Win32_LogicalDisk -InputObject $partition foreach($drive in $drives) { $totalSpace = [math]::Round($drive.Size / 1GB, 3) $freeSpace = [math]::Round($drive.FreeSpace / 1GB, 3) $usedSpace = [math]::Round($totalSpace - $freeSpace, 3) $volume = Get-Volume | Where-Object { $_.DriveLetter -eq $drive.DeviceID.Trim(":") } | Select-Object -First 1 [PSCustomObject] @{ Number = $disk.Index UsedSpace = $usedSpace } } } } } Get-Space
It returns the following results:
Number UsedSpace ------ --------- 1 326,045 2 363,873 3 0 0 228,094
Now I need a c ++ function which based on this data (the data is saved as QStringList - each line separately) will create a QStringList named disks which will assign values to them based on disk indexes. For example:
QStringList disks;
disks [1] = 326,045 // This is disk 1
disks [2] = 363.873 // This is disk 2It should be something like this:
disks [1] = returned value of disk 1I just don't know how to extract this data from the returned data.
-
@Piotrek102 Open the file (you mentioned file, right?) then read it line by line adding to QStringList.
Btw., please avoid using [] indexes, use insert() and at() methods to add and read the content.
You can use QFile::readLine() for reading the file. -
@Piotrek102
Did you see the UPDATE I added to my earlier post, aboutQStorageInfo
? If that gives you the same information as you want from your PS script, it will be easier to use than running a script and parsing the output. Up to you..... -
I use my own function to work with QProcess:
#include "mainwindow.h" #include "ui_mainwindow.h" bool process_ok; QStringList to_return; void MainWindow::encodingFinished() { process_ok=true; } void MainWindow::readyReadStandardOutput() { to_return << process->readAllStandardOutput(); } QStringList MainWindow::RunCommand(QString cmd, QStringList commands) { to_return.clear(); process_ok=false; process->start(cmd, commands); while(process_ok!=true) delay_MSec(100); return to_return; }
So to call the ps1 script I use it like this:
QStringList returned; command = qApp->applicationDirPath()+"/scripts/getdisk.ps1"; commands << command; returned = RunCommand("powershell.exe", commands);
-
@JonB
I will check it, thank you for the hint -
@Piotrek102 said in QProcess and powershell - checking if the disk is clean:
while(process_ok!=true) delay_MSec(100);
You must/should not do this sort of thing in Qt programming. If you really want to wait, you must use
QProcess::waitForFinished()
. -
@JonB
That's right, I know it, but QProcess :: waitForFinished () was freezing my gui. this feature doesn't do that and works fine, I have tested it multiple times in my other programs. I know this is not correct but it works.void MainWindow::delay_MSec(unsigned int msec) { QTime _Timer = QTime::currentTime().addMSecs(msec); while( QTime::currentTime() < _Timer ) QCoreApplication::processEvents(QEventLoop::AllEvents, 100); }
To be clear, this is not my function, I found it on the internet
@JonB
As for your suggestion, QStorageInfo is what I need! It works very well. I have only one question. Is there also an option to specify a disk index - e.g. disk 0?foreach (const QStorageInfo &storage, QStorageInfo::mountedVolumes()) { if (storage.isValid() && storage.isReady()) { if (!storage.isReadOnly()) { qDebug() << "name:" << storage.name(); qDebug() << "fileSystemType:" << storage.fileSystemType(); qDebug() << "size:" << storage.bytesTotal()/1000/1000 << "MB"; qDebug() << "availableSize:" << storage.bytesAvailable()/1000/1000 << "MB"; //qDebug() << index of disk here too } } }
-
@Piotrek102 said in QProcess and powershell - checking if the disk is clean:
QProcess :: waitForFinished () was freezing my gui
void MainWindow::delay_MSec(unsigned int msec)
I did not know what was in your
delay_MSec()
. UsingprocessEvents()
like this is not ideal. The best way is no synchronicity/waits/loops/processEvents, rather let it run asynchronously and continue your code in your slot onQProcess::finished()
signal.If the code works for you fair enough. It's moot anyway if you choose to use
QStorageInfo
instead.Is there also an option to specify a disk index - e.g. disk 0?
I know no more than whatever is in
QStorageInfo
doc page. I have never used it :) [You can use an integer indexer intomountedVolumes()
instead offoreach
of that's all your "disk index - e.g. disk 0" is, I don't know what ordermountedVolumes()
returns them in?] -
@Piotrek102 Please read the documentation.
You can list mounted volumes using mountedVolumes() static method.
Also please drop
foreach
, it's obsolete. Just iterate over items in the list. -
@Piotrek102 Do this in a thread to avoid any delay call(==>as less as possible) since it may take a while to get all storage info. GUI is not blocked in this way.
-
This post is deleted!