File in use (Windows.h)

So I'm making a program that copies files as it detects them to another directory. The issue is that if I FileCopy() too soon it will only get a part of the file if the file is still being written to.

I know that I can get the files size and use that to find out if the file is in use but I'm looking for a faster and more elegant way of doing it.

So yeah, any hidden functions I should know about ?

PS. I am using windows.h and derent.h for file stuff in my program.

Also this is my first post on this forum so if iv screwed something up please inform me.
SH4773R wrote:
So I'm making a program that copies files as it detects them to another directory. The issue is that if I FileCopy() too soon it will only get a part of the file if the file is still being written to.

I know that I can get the files size and use that to find out if the file is in use but I'm looking for a faster and more elegant way of doing it.

So yeah, any hidden functions I should know about ?

PS. I am using windows.h and derent.h for file stuff in my program.

Also this is my first post on this forum so if iv screwed something up please inform me.


Well, I suppose you mean CopyFile() and not FileCopy() since the latter is not a Windows API function name.

The way I would do it would be to use CreateFile() to open the file with FILE_SHARE_READ and OPEN_EXISTING. This call should fail if the file is being written to. Since I don't know of a way to wait on a file system object (like a file), I would implement this function call inside a loop and combine it with Sleep(100). The loop would exit when CreateFile() succeeds.

Once CreateFile() succeeds you can proceed with CopyFile(), and once CopyFile() completes you close the handle returned by CreateFile().
Last edited on
I do Indeed mean CopyFile(), sorry.

Thanks for the quick answer. I'll work on implementing your method, but a example would still be nice... :D
Never Mind, I figured it out :)
Topic archived. No new replies allowed.