Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to monitor files on an external device?
Qt 6.11 is out! See what's new in the release blog

How to monitor files on an external device?

Scheduled Pinned Locked Moved Unsolved General and Desktop
17 Posts 5 Posters 2.5k Views 2 Watching
  • 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.
  • mrjjM mrjj

    @Blaster
    Hi
    you mean you just need to calculate the amount of MB on the drive ?
    With the added requirement that files already on drive, even if moved around
    to new folder or renamed, do not count to that total?

    BlasterB Offline
    BlasterB Offline
    Blaster
    wrote on last edited by Blaster
    #6

    @mrjj Yes, that's it. Initially it had occurred to me to see the size when putting the device and then verify it when extracting it, but then the calculation fails when files are deleted.

    mrjjM 1 Reply Last reply
    0
    • BlasterB Blaster

      @mrjj Yes, that's it. Initially it had occurred to me to see the size when putting the device and then verify it when extracting it, but then the calculation fails when files are deleted.

      mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #7

      @Blaster
      If think you need to use CRC for each file and store in a database.
      This way you can discover if file has been moved or renamed and/or changed.
      https://stackoverflow.com/questions/27939882/fast-crc-algorithm

      BlasterB 1 Reply Last reply
      1
      • J Offline
        J Offline
        jayden982
        Banned
        wrote on last edited by jayden982
        #8
        This post is deleted!
        1 Reply Last reply
        0
        • mrjjM mrjj

          @Blaster
          If think you need to use CRC for each file and store in a database.
          This way you can discover if file has been moved or renamed and/or changed.
          https://stackoverflow.com/questions/27939882/fast-crc-algorithm

          BlasterB Offline
          BlasterB Offline
          Blaster
          wrote on last edited by
          #9

          @mrjj but how do I get the amount of megas that are created from the new files? In addition, the process of checking each file is too expensive in time

          JonBJ 1 Reply Last reply
          0
          • BlasterB Blaster

            @mrjj but how do I get the amount of megas that are created from the new files? In addition, the process of checking each file is too expensive in time

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

            @Blaster
            When I first saw this question days ago I would have suggested QFileSystemWatcher. I'm sure someone did but there's no mention. So I wonder why @VRonin has deleted a reply, which I would have guessed suggested that?

            Anyway, let's restart. You're going to have to think out just what you want/can live with, because nothing is going to do exactly as you ask, because there's so many things you can do with files it's hard to define just what you will want here.

            I agree you won't want to have examine file contents for CRC, too slow.

            Most importantly: when do you need to monitor/examine this information?

            • If you need to in real-time, as the user is doing things, can you leverage QFileSystemWatcher for this device? I don't know whether it will support it.

            • If you only need to discover this when your program is run, in some shape or form you want to save a "database" of what was there before and compare it to what is there now.

            Which approach?

            BTW, just maybe, if all you want to know about is the "new" ones, you could simply look at the files' "creation time" attribute, if that's acceptable for your purposes?

            VRoninV BlasterB 2 Replies Last reply
            1
            • JonBJ JonB

              @Blaster
              When I first saw this question days ago I would have suggested QFileSystemWatcher. I'm sure someone did but there's no mention. So I wonder why @VRonin has deleted a reply, which I would have guessed suggested that?

              Anyway, let's restart. You're going to have to think out just what you want/can live with, because nothing is going to do exactly as you ask, because there's so many things you can do with files it's hard to define just what you will want here.

              I agree you won't want to have examine file contents for CRC, too slow.

              Most importantly: when do you need to monitor/examine this information?

              • If you need to in real-time, as the user is doing things, can you leverage QFileSystemWatcher for this device? I don't know whether it will support it.

              • If you only need to discover this when your program is run, in some shape or form you want to save a "database" of what was there before and compare it to what is there now.

              Which approach?

              BTW, just maybe, if all you want to know about is the "new" ones, you could simply look at the files' "creation time" attribute, if that's acceptable for your purposes?

              VRoninV Offline
              VRoninV Offline
              VRonin
              wrote on last edited by
              #11

              @JonB said in How to monitor files on an external device?:

              So I wonder why @VRonin has deleted a reply

              Because QFileSystemWatcher can't differentiate about what is happening to the file so the solution is not as easy as it seams

              "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
              ~Napoleon Bonaparte

              On a crusade to banish setIndexWidget() from the holy land of Qt

              JonBJ 1 Reply Last reply
              0
              • VRoninV VRonin

                @JonB said in How to monitor files on an external device?:

                So I wonder why @VRonin has deleted a reply

                Because QFileSystemWatcher can't differentiate about what is happening to the file so the solution is not as easy as it seams

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

                @VRonin
                Then, my friend, an edit to the post explaining why not might have better than a delete :)

                I still think that's where you'd start from if you needed it in real-time. And I'm asking the OP to think about just what he wants to know. Maybe all he needs to know about is new file creation, and he'll ignore rename/move? He needs to clarify whether real-time or one-off run program is what he wants.

                1 Reply Last reply
                1
                • VRoninV Offline
                  VRoninV Offline
                  VRonin
                  wrote on last edited by
                  #13

                  actually, maybe there's a easy way:

                  Untested Code

                  // QFileSystemWatcher* fwatcher = new QFileSystemWatcher;
                  QDir targetDir("path/to/folder/");
                  fwatcher->addPath(targetDir.canonicalPath());
                  // private: uint oldCount;
                  oldCount = targetDir.count();
                  QObject::connect(fwatcher,&QFileSystemWatcher::directoryChanged,[targetDir,&oldCount](const QString& path)->void{
                  if(path!=targetDir.canonicalPath()) return;
                  if(oldCount<targetDir.count()) qDebug() << "New File Added";
                  oldCount=targetDir.count();
                  });
                  

                  "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
                  ~Napoleon Bonaparte

                  On a crusade to banish setIndexWidget() from the holy land of Qt

                  BlasterB 1 Reply Last reply
                  3
                  • VRoninV VRonin

                    actually, maybe there's a easy way:

                    Untested Code

                    // QFileSystemWatcher* fwatcher = new QFileSystemWatcher;
                    QDir targetDir("path/to/folder/");
                    fwatcher->addPath(targetDir.canonicalPath());
                    // private: uint oldCount;
                    oldCount = targetDir.count();
                    QObject::connect(fwatcher,&QFileSystemWatcher::directoryChanged,[targetDir,&oldCount](const QString& path)->void{
                    if(path!=targetDir.canonicalPath()) return;
                    if(oldCount<targetDir.count()) qDebug() << "New File Added";
                    oldCount=targetDir.count();
                    });
                    
                    BlasterB Offline
                    BlasterB Offline
                    Blaster
                    wrote on last edited by
                    #14

                    @VRonin What you propose only returns me the amount of new files in the directory and what I need is the amount of space it occupies, but that's the point ... I think I can go from there

                    JonBJ 1 Reply Last reply
                    0
                    • JonBJ JonB

                      @Blaster
                      When I first saw this question days ago I would have suggested QFileSystemWatcher. I'm sure someone did but there's no mention. So I wonder why @VRonin has deleted a reply, which I would have guessed suggested that?

                      Anyway, let's restart. You're going to have to think out just what you want/can live with, because nothing is going to do exactly as you ask, because there's so many things you can do with files it's hard to define just what you will want here.

                      I agree you won't want to have examine file contents for CRC, too slow.

                      Most importantly: when do you need to monitor/examine this information?

                      • If you need to in real-time, as the user is doing things, can you leverage QFileSystemWatcher for this device? I don't know whether it will support it.

                      • If you only need to discover this when your program is run, in some shape or form you want to save a "database" of what was there before and compare it to what is there now.

                      Which approach?

                      BTW, just maybe, if all you want to know about is the "new" ones, you could simply look at the files' "creation time" attribute, if that's acceptable for your purposes?

                      BlasterB Offline
                      BlasterB Offline
                      Blaster
                      wrote on last edited by
                      #15

                      @JonB The truth, I had not thought about the date of creation. I think the most viable option is to use the QFileSystemWatcher and verify the date. I think that could work ... Thanks friend. I will check for this

                      1 Reply Last reply
                      0
                      • BlasterB Blaster

                        @VRonin What you propose only returns me the amount of new files in the directory and what I need is the amount of space it occupies, but that's the point ... I think I can go from there

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

                        @Blaster said in How to monitor files on an external device?:

                        @VRonin What you propose only returns me the amount of new files in the directory and what I need is the amount of space it occupies, but that's the point ... I think I can go from there

                        QFileSystemWatcher returns path of file changed. The important thing is that only fires when a change is being made. (Remember, you still need to test whatever your "device" is to make sure it fires change events.) You would use a QFileInfo on that to get the size information you are interested in. @VRonin's count() is only an example. This combination is as efficient as possible.

                        EDIT
                        Wait, I see, I get it now. QFileSystemWatcher won't tell you which file(s). OK, that's problematic.

                        You'd have to do something like: iterate all children by creation time to spot which ones are new. At least you'll only do it when something gets added or deleted, but not great.

                        Go back to question: do you need to know in real-time when user does something, or just once at some specified point when they run an app of yours?

                        P.S. What OS? If Windows, and you find you really need this and have to roll your own, maybe some of the suggestions in https://stackoverflow.com/questions/2107275/does-anyone-have-a-filesystemwatcher-like-class-in-c-winapi would help? Better: https://developersarea.wordpress.com/2014/09/26/win32-file-watcher-api-to-monitor-directory-changes/ And the WinAPI is around https://docs.microsoft.com/en-gb/windows/desktop/api/winbase/nf-winbase-readdirectorychangesw , that leads to event "FILE_ACTION_ADDED 0x00000001 The file was added to the directory. ". You may need the Win level to get exactly what you want specifically rather than QFileSystemWatcher.

                        Otherwise, this might sound like a journaling requirement? It's all getting a bit hairy...!

                        BlasterB 1 Reply Last reply
                        2
                        • JonBJ JonB

                          @Blaster said in How to monitor files on an external device?:

                          @VRonin What you propose only returns me the amount of new files in the directory and what I need is the amount of space it occupies, but that's the point ... I think I can go from there

                          QFileSystemWatcher returns path of file changed. The important thing is that only fires when a change is being made. (Remember, you still need to test whatever your "device" is to make sure it fires change events.) You would use a QFileInfo on that to get the size information you are interested in. @VRonin's count() is only an example. This combination is as efficient as possible.

                          EDIT
                          Wait, I see, I get it now. QFileSystemWatcher won't tell you which file(s). OK, that's problematic.

                          You'd have to do something like: iterate all children by creation time to spot which ones are new. At least you'll only do it when something gets added or deleted, but not great.

                          Go back to question: do you need to know in real-time when user does something, or just once at some specified point when they run an app of yours?

                          P.S. What OS? If Windows, and you find you really need this and have to roll your own, maybe some of the suggestions in https://stackoverflow.com/questions/2107275/does-anyone-have-a-filesystemwatcher-like-class-in-c-winapi would help? Better: https://developersarea.wordpress.com/2014/09/26/win32-file-watcher-api-to-monitor-directory-changes/ And the WinAPI is around https://docs.microsoft.com/en-gb/windows/desktop/api/winbase/nf-winbase-readdirectorychangesw , that leads to event "FILE_ACTION_ADDED 0x00000001 The file was added to the directory. ". You may need the Win level to get exactly what you want specifically rather than QFileSystemWatcher.

                          Otherwise, this might sound like a journaling requirement? It's all getting a bit hairy...!

                          BlasterB Offline
                          BlasterB Offline
                          Blaster
                          wrote on last edited by
                          #17

                          @JonB Exact. It is a bit tangled.
                          I need to know the volume of information, by flash drive inserted in the pc. It is not necessary for the user to know that the new file was registered in the application, only those who have the administration password of the application should know it, when they open it. The OS is Windows.

                          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