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?
Forum Updated to NodeBB v4.3 + New Features

How to monitor files on an external device?

Scheduled Pinned Locked Moved Unsolved General and Desktop
17 Posts 5 Posters 2.2k 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.
  • B Blaster
    22 Oct 2018, 20:36

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

    M Offline
    M Offline
    mrjj
    Lifetime Qt Champion
    wrote on 22 Oct 2018, 21:13 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

    B 1 Reply Last reply 23 Oct 2018, 15:59
    1
    • J Offline
      J Offline
      jayden982
      Banned
      wrote on 22 Oct 2018, 21:21 last edited by jayden982
      #8
      This post is deleted!
      1 Reply Last reply
      0
      • M mrjj
        22 Oct 2018, 21:13

        @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

        B Offline
        B Offline
        Blaster
        wrote on 23 Oct 2018, 15:59 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

        J 1 Reply Last reply 23 Oct 2018, 16:53
        0
        • B Blaster
          23 Oct 2018, 15:59

          @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

          J Offline
          J Offline
          JonB
          wrote on 23 Oct 2018, 16:53 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?

          V B 2 Replies Last reply 23 Oct 2018, 16:59
          1
          • J JonB
            23 Oct 2018, 16:53

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

            V Offline
            V Offline
            VRonin
            wrote on 23 Oct 2018, 16:59 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

            J 1 Reply Last reply 23 Oct 2018, 17:05
            0
            • V VRonin
              23 Oct 2018, 16:59

              @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

              J Offline
              J Offline
              JonB
              wrote on 23 Oct 2018, 17:05 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
              • V Offline
                V Offline
                VRonin
                wrote on 23 Oct 2018, 17:19 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

                B 1 Reply Last reply 23 Oct 2018, 19:45
                3
                • V VRonin
                  23 Oct 2018, 17:19

                  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();
                  });
                  
                  B Offline
                  B Offline
                  Blaster
                  wrote on 23 Oct 2018, 19:45 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

                  J 1 Reply Last reply 23 Oct 2018, 19:55
                  0
                  • J JonB
                    23 Oct 2018, 16:53

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

                    B Offline
                    B Offline
                    Blaster
                    wrote on 23 Oct 2018, 19:47 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
                    • B Blaster
                      23 Oct 2018, 19:45

                      @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

                      J Offline
                      J Offline
                      JonB
                      wrote on 23 Oct 2018, 19:55 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...!

                      B 1 Reply Last reply 24 Oct 2018, 17:19
                      2
                      • J JonB
                        23 Oct 2018, 19:55

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

                        B Offline
                        B Offline
                        Blaster
                        wrote on 24 Oct 2018, 17:19 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

                        16/17

                        23 Oct 2018, 19:55

                        • Login

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