How to know a file is finished copying
Asked Answered
S

3

2

I’m using ReadDirectoryChangesW to spy a folder, if I’m copying a large file to the folder, I can receive multiple FILE_ACTION_MODIFIED messages, it seems each time windows writes a large chunk of the file, you get the file modified notification for each time. I tried to use CreateFile API to check if the file can open by AP or not, but sometime, some of files are always locked by other AP, for example, if you are opening the Outlook, the PST will update, but my AP can’t access it, we have to start Shadow Copy to open it. So my question is, how to know a file is finished copying?

Spermiogenesis answered 18/2, 2009 at 2:46 Comment(0)
L
1

This is a solution of a very hacky nature, but in a very hairy situation you can run a polling loop to check the size (or modified date) of the file. If it doesn't change for a while, you can somewhat safely assume that the file is finished copying.

EDIT: This is not the optimal solution in the average case, but consider that in the OP's case better solutions are ruled out by the problem constraints. Think about this before you think of downvoting.

Lemuellemuela answered 18/2, 2009 at 3:2 Comment(0)
S
0

When you poll for the size of the file, don't use _stat/_stat64. Instead, open the file each time you need and call _filelengthi64 to get the size of the file. The file size that is returned by _stat64 is not updated in real-time by the Windows operating system. Also by being able to open the file, you are testing to see that any long copy operations have been completed.

Sciurine answered 7/5, 2011 at 6:20 Comment(0)
C
-1

Windows provides an API to monitor directory content update. You can use the Created event to detect new file, but be aware that this event firing does not necessarily means that the file is released yet.

native C++ specs and code example :

http://msdn.microsoft.com/en-us/library/aa365261(VS.85).aspx

.net :

http://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher.aspx

Capping answered 14/5, 2014 at 8:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.