Results 1 to 3 of 3

Thread: How to detect a file being successfully created in a folder

  1. #1
    Join Date
    Apr 2009
    Posts
    11
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Windows

    Default How to detect a file being successfully created in a folder

    Hi,

    Currently my application sends a message to a third party program to ask it to do some sort of image capturing to the file name and path of my choice. However, I am having difficulty trying to determine when the file has been successfully created in my folder. I tried using the QFileSystemWatcher to monitor the folder in which I expect the file to be created. But the notification comes too early, in the sense that the file is still being written to when I try to open it for reading. Any ideas/suggestions how I can detect the file after it has been successfully created? Thanks for your help.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to detect a file being successfully created in a folder

    Can the writer notify you of the completed task through the same channel that your "application sends a message to a third party program" through?

    If you are spawning the third-party program as a process that terminates when finished then wait on that event before opening the file. If that program can output to standard output then you can do the whole thing through QProcess.

    If you have control of the writer application you can have the writer create two files, file.dat and file.dat.lock, and delete the lock file when finished writing the main file. When the lock file disappears the file is ready. Of course, if you have control of the writer a shared memory segment would be another option.

    If you do not have control of the writer you could attempt to open the file for read/write access. If the open fails then back off for a few seconds and try again. Each time it fails wait a little longer (up to some maximum). When the open succeeds the writer's lock on the file has been released. Busy polling is not pretty but if the average processing time is fairly well known then you can tune it it rarely need to retry.

  3. #3
    Join Date
    Jan 2006
    Location
    Belgium
    Posts
    1,938
    Thanked 268 Times in 268 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Wiki edits
    20

    Default Re: How to detect a file being successfully created in a folder

    In the rare case that the system lets you open a file in read/write mode while the other process is also opening the file in read/write mode, you can try to set a lock yourself. If you can't set a lock, another process has access to the file? Or, this can go wrong and leave your own program with the lock while the other can't write to it?

Similar Threads

  1. Save File in another Folder
    By GonzoFist in forum General Programming
    Replies: 14
    Last Post: 20th April 2010, 12:59
  2. Moving File/Folder in Qt
    By arpspatel in forum Qt Programming
    Replies: 4
    Last Post: 17th January 2010, 01:37
  3. Release folder - 1 .exe file only
    By Swankee in forum Qt Tools
    Replies: 6
    Last Post: 21st December 2009, 22:43
  4. Best way to check if file is successfully sent over QTcpSocket
    By cutie.monkey in forum Qt Programming
    Replies: 5
    Last Post: 17th October 2009, 20:24
  5. How to detect if a file is being used?
    By richardander in forum Qt Programming
    Replies: 3
    Last Post: 11th June 2009, 19:30

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.