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
QtWS25 Last Chance

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 Piotrek102
    6 Aug 2021, 12:24

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

    J Offline
    J Offline
    JonB
    wrote on 6 Aug 2021, 13:05 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 6 Aug 2021, 13:08 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);
      
      J 1 Reply Last reply 6 Aug 2021, 13:28
      0
      • P Offline
        P Offline
        Piotrek102
        wrote on 6 Aug 2021, 13:23 last edited by
        #12

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

        1 Reply Last reply
        0
        • P Piotrek102
          6 Aug 2021, 13:08

          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);
          
          J Offline
          J Offline
          JonB
          wrote on 6 Aug 2021, 13:28 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 6 Aug 2021, 13:36
          0
          • J JonB
            6 Aug 2021, 13:28

            @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 6 Aug 2021, 13:36 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
            			}
            		}
            	}
            
            J A J 3 Replies Last reply 6 Aug 2021, 13:44
            0
            • P Piotrek102
              6 Aug 2021, 13:36

              @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
              			}
              		}
              	}
              
              J Offline
              J Offline
              JonB
              wrote on 6 Aug 2021, 13:44 last edited by JonB 8 Jun 2021, 13:53
              #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 6 Aug 2021, 13:47
              1
              • J JonB
                6 Aug 2021, 13:44

                @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 6 Aug 2021, 13:47 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
                  6 Aug 2021, 13:36

                  @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
                  			}
                  		}
                  	}
                  
                  A Offline
                  A Offline
                  artwaw
                  wrote on 6 Aug 2021, 13:47 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
                    6 Aug 2021, 13:36

                    @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
                    			}
                    		}
                    	}
                    
                    J Offline
                    J Offline
                    JoeCFD
                    wrote on 6 Aug 2021, 15:54 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
                      5 Aug 2021, 16:03

                      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 30 Oct 2021, 06:46 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