How to wait until Explorer finishes copying a file (Solved)
-
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
-
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
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.
-
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
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
-
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
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