Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to wait until Explorer finishes copying a file (Solved)

How to wait until Explorer finishes copying a file (Solved)

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestiontutorial
4 Posts 3 Posters 0 Views 1 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.
  • S Offline
    S Offline
    ShadowUz
    wrote on last edited by
    #1

    Hello everyone. I am doing a sync program that has a spying function. The spying function will detect any changes in the Source folder and will sync it with the Destination folder as soon as detects any changes. My problem is, when a new file is added into the Source folder, the sync function immediately will start copying the new file to the Destination folder. And, if the file is still being used by explorer for writing, the program gives an error. As the file has not been fully written. How can I make the sync function wait for the explorer, until it finishes copying the file?

    modified on Thursday, November 25, 2010 8:01 PM

    L L 2 Replies Last reply
    0
    • S ShadowUz

      Hello everyone. I am doing a sync program that has a spying function. The spying function will detect any changes in the Source folder and will sync it with the Destination folder as soon as detects any changes. My problem is, when a new file is added into the Source folder, the sync function immediately will start copying the new file to the Destination folder. And, if the file is still being used by explorer for writing, the program gives an error. As the file has not been fully written. How can I make the sync function wait for the explorer, until it finishes copying the file?

      modified on Thursday, November 25, 2010 8:01 PM

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      I know the problem, and there isn't a perfect solution AFAIK. What I tend to do, whenever possible, is having the producer produce another very small file once the actual files have been handled, and use that as a signal. This of course only works when I have some say it what the producer does. When I don't, the only thing I have is polling (a wait-and-try loop). :)

      Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles] Nil Volentibus Arduum

      Please use <PRE> tags for code snippets, they preserve indentation, and improve readability.

      1 Reply Last reply
      0
      • S ShadowUz

        Hello everyone. I am doing a sync program that has a spying function. The spying function will detect any changes in the Source folder and will sync it with the Destination folder as soon as detects any changes. My problem is, when a new file is added into the Source folder, the sync function immediately will start copying the new file to the Destination folder. And, if the file is still being used by explorer for writing, the program gives an error. As the file has not been fully written. How can I make the sync function wait for the explorer, until it finishes copying the file?

        modified on Thursday, November 25, 2010 8:01 PM

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        mints555 wrote:

        And, if the file is still being used by explorer for writing, the program gives an error.

        I would recommend that you attempt to open the file with exclusive access when copying. The OS should deny your request if the handle is being referenced by another process. Best Wishes, -David Delaune

        S 1 Reply Last reply
        0
        • L Lost User

          mints555 wrote:

          And, if the file is still being used by explorer for writing, the program gives an error.

          I would recommend that you attempt to open the file with exclusive access when copying. The OS should deny your request if the handle is being referenced by another process. Best Wishes, -David Delaune

          S Offline
          S Offline
          ShadowUz
          wrote on last edited by
          #4

          Thanks for the advice David. I solved the problem by the code shown below:

          CFile sour;
          CString source=_T("Path to file");

          while (!sour.Open(source,CFile::modeRead | CFile::shareDenyNone,NULL))
          {
          Sleep(1000);
          }

          sour.Close();

          modified on Thursday, November 25, 2010 3:34 AM

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups