Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. General talk
  3. Brainstorm
  4. QProcess and powershell - checking if the disk is clean

QProcess and powershell - checking if the disk is clean

Scheduled Pinned Locked Moved Solved Brainstorm
19 Posts 6 Posters 2.3k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Piotrek102
    wrote on last edited by
    #1

    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!

    jsulmJ B 2 Replies Last reply
    0
    • P Piotrek102

      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!

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @Piotrek102 What exactly is your question/problem?
      What did you try?

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      1
      • P Offline
        P Offline
        Piotrek102
        wrote on last edited by
        #3

        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.

        jsulmJ 1 Reply Last reply
        0
        • P Piotrek102

          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.

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @Piotrek102 You did not answer any of my questions.
          Again: what did you try and what is the problem/question?

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • P Offline
            P Offline
            Piotrek102
            wrote on last edited by
            #5

            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
            ...

            JonBJ artwawA 3 Replies Last reply
            0
            • P Piotrek102

              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
              ...

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #6

              @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 a static QList<QStorageInfo> mountedVolumes(), if that gives you the information you seek.

              1 Reply Last reply
              1
              • P Piotrek102

                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
                ...

                artwawA Offline
                artwawA Offline
                artwaw
                wrote on last edited by
                #7

                @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.

                For more information please re-read.

                Kind Regards,
                Artur

                1 Reply Last reply
                0
                • P Offline
                  P Offline
                  Piotrek102
                  wrote on last edited by
                  #8

                  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 2

                  It should be something like this:
                  disks [1] = returned value of disk 1

                  I just don't know how to extract this data from the returned data.

                  artwawA 1 Reply Last reply
                  0
                  • P Piotrek102

                    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 2

                    It should be something like this:
                    disks [1] = returned value of disk 1

                    I just don't know how to extract this data from the returned data.

                    artwawA Offline
                    artwawA Offline
                    artwaw
                    wrote on last edited by
                    #9

                    @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.

                    For more information please re-read.

                    Kind Regards,
                    Artur

                    1 Reply Last reply
                    0
                    • P Piotrek102

                      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
                      ...

                      JonBJ Offline
                      JonBJ Offline
                      JonB
                      wrote on last edited by
                      #10

                      @Piotrek102
                      Did you see the UPDATE I added to my earlier post, about QStorageInfo? 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.....

                      1 Reply Last reply
                      1
                      • P Offline
                        P Offline
                        Piotrek102
                        wrote on last edited by
                        #11

                        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);
                        
                        JonBJ 1 Reply Last reply
                        0
                        • P Offline
                          P Offline
                          Piotrek102
                          wrote on last edited by
                          #12

                          @JonB
                          I will check it, thank you for the hint

                          1 Reply Last reply
                          0
                          • P Piotrek102

                            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);
                            
                            JonBJ Offline
                            JonBJ Offline
                            JonB
                            wrote on last edited by
                            #13

                            @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().

                            P 1 Reply Last reply
                            0
                            • JonBJ JonB

                              @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().

                              P Offline
                              P Offline
                              Piotrek102
                              wrote on last edited by
                              #14

                              @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
                              			}
                              		}
                              	}
                              
                              JonBJ artwawA JoeCFDJ 3 Replies Last reply
                              0
                              • P Piotrek102

                                @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
                                			}
                                		}
                                	}
                                
                                JonBJ Offline
                                JonBJ Offline
                                JonB
                                wrote on last edited by JonB
                                #15

                                @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(). Using processEvents() 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 on QProcess::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 into mountedVolumes() instead of foreach of that's all your "disk index - e.g. disk 0" is, I don't know what order mountedVolumes() returns them in?]

                                P 1 Reply Last reply
                                1
                                • JonBJ JonB

                                  @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(). Using processEvents() 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 on QProcess::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 into mountedVolumes() instead of foreach of that's all your "disk index - e.g. disk 0" is, I don't know what order mountedVolumes() returns them in?]

                                  P Offline
                                  P Offline
                                  Piotrek102
                                  wrote on last edited by
                                  #16

                                  Thank you very much for all your help. Like @JonB said I will try with QStorageInfo. Thanks again for your help!

                                  1 Reply Last reply
                                  0
                                  • P Piotrek102

                                    @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
                                    			}
                                    		}
                                    	}
                                    
                                    artwawA Offline
                                    artwawA Offline
                                    artwaw
                                    wrote on last edited by
                                    #17

                                    @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.

                                    For more information please re-read.

                                    Kind Regards,
                                    Artur

                                    1 Reply Last reply
                                    1
                                    • P Piotrek102

                                      @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
                                      			}
                                      		}
                                      	}
                                      
                                      JoeCFDJ Offline
                                      JoeCFDJ Offline
                                      JoeCFD
                                      wrote on last edited by
                                      #18

                                      @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.

                                      1 Reply Last reply
                                      0
                                      • P Piotrek102

                                        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!

                                        B Offline
                                        B Offline
                                        Bazooca
                                        Banned
                                        wrote on last edited by
                                        #19
                                        This post is deleted!
                                        1 Reply Last reply
                                        0

                                        • Login

                                        • Login or register to search.
                                        • First post
                                          Last post
                                        0
                                        • Categories
                                        • Recent
                                        • Tags
                                        • Popular
                                        • Users
                                        • Groups
                                        • Search
                                        • Get Qt Extensions
                                        • Unsolved