Re: FileCopy vs. Read & Write or CopyFile API

Hi Saga

Thanks for your reply.

The files on the "server side" (the source files) are release versions of
formated document text. No one has these documents opened (they have a fake
suffix), so the only cause for a file-lock is that another client iscopying
the file at that perticular moment.
With your and Jialiang Ges other solution, the possibility that yet another
client is copying the file in 5 or 10 seconds is to big.
With FileSystemObjects copy-function, the correct version of the file will be
copied even if another client is copying it at the same time.


Peter Karlström
Midrange AB

"Saga" wrote:

The first thing I would ask is... why is the file locked? If it is in use, do you
really want to copy it, considering that it may not be in a stable state?

I like Jialiang's solution to loop whiel there is an error copying the file, but I
would add a time loop so that you can define how long you want the code
to loop with error before giving up:

On Error Resume Next
Dim errNum as Integer
Dim dteFuture as date

errNum = 1
dteFuture = dateadd("s",10,now) 'Try for 10 seconds, change at will<g>

While errNum > 0 and dtefuture > Now
FileCopy "server file", "client file"
errNum = Err.Number

Also, if you decide to use FSO, I suggest doing a Google search in this group
to see how this has worked for others. Many here advise against it since it brings
its own set of issues.


"Peter Karlström" <HayRoller@xxxxxxxxxxxxxxxx> wrote in message
Hi Jialiang Ge

Thanks for your quick reply

You understod my problem correctly.

I will switch the copy-function to the one in FileSystemObject.
It seems that if file-locks is the problem this will solve it.

Unfortunately this will take a while to test in the clients environment,
since we must
deploy a new version, but if it doesn't work I will post a new question to
this forum.

Again, thanks

Peter Karlström
Midrange AB

"Jialiang Ge [MSFT]" wrote:

Hello Peter,

From your post, my understanding on this issue is: you wonder how to
synchronize a document in server to its client with VB FileCopy function.
If I'm off base, please feel free to let me know.

Generally speaking, VB function FileCopy fails and pop out "Permission
denied" exception when we attempt to copy a file that is opened or when the
destination file cannot be overwritten. Please check if the document in the
server is opened, and if the destination in client can be overwritten.

If the file in the server is opened for a short time, in the client, we
could loop to check if the file is locked:
On Error Resume Next
Dim errNum as Integer
errNum = 1
While errNum > 0
FileCopy "server file", "client file"
errNum = Err.Number
As long as the file is closed, the loop will stop and the file will be

Another approach is to use FileSystemObject
( , which can copy a
locked file:
Set fs = CreateObject("Scripting.FileSystemObject")
fs.copyfile "server file", "client file"

It copies the last save of the file.
Please have a try and let me know the result.

if you have any other concerns, or need anything else, please feel free to
let me know.

Jialiang Ge (jialge@xxxxxxxxxxxxxxxxxxxx, remove 'online.')
Microsoft Online Community Support

For MSDN subscribers whose posts are left unanswered, please check this

Get notification to my posts through email? Please refer to
ications. If you are using Outlook Express/Windows Mail, please make sure
you clear the check box "Tools/Options/Read: Get 300 headers at a time" to
see your reply promptly.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
This posting is provided "AS IS" with no warranties, and confers no rights.