|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
FileCopy vs. Read & Write or CopyFile APIWe have tried creating an automatic update function in one of our COM Addins for Word. We want to update data in a database and copy Word doc-files which we specify in the database. The files are located on a network share and the permissions are allright. The clients have local copies of the database and the Word-files, and the update goal is to make sure the clients have the latest version of files and data. The data-update works fine, but it seems that the copying of files doesn't allways work. The COM Addin is deployed in an organisation with 200+ clients, and I'm suspecting file-locks on the files in question. The files could be of any size from 30 kB to 800 kB. We use the VB FileCopy function to do the actual copy. Is this the wrong approach, and if so, is there a more propriate solution, like API-function CopyFile or a regular file-read and write? The COM Addin is created in VB6. Thanks in advance -- Peter Karlström Midrange AB Sweden 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 Err.Clear Wend As long as the file is closed, the loop will stop and the file will be copied. Another approach is to use FileSystemObject (http://msdn2.microsoft.com/en-us/library/2z9ffy99.aspx) , 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. Sincerely, Jialiang Ge (jia***@online.microsoft.com, remove 'online.') Microsoft Online Community Support ================================================== For MSDN subscribers whose posts are left unanswered, please check this document: http://blogs.msdn.com/msdnts/pages/postingAlias.aspx Get notification to my posts through email? Please refer to http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif 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 http://msdn.microsoft.com/subscriptions/support/default.aspx. ================================================== This posting is provided "AS IS" with no warranties, and confers no rights. 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 -- Show quotePeter Karlström Midrange AB Sweden "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 > Err.Clear > Wend > As long as the file is closed, the loop will stop and the file will be > copied. > > Another approach is to use FileSystemObject > (http://msdn2.microsoft.com/en-us/library/2z9ffy99.aspx) , 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. > > Sincerely, > Jialiang Ge (jia***@online.microsoft.com, remove 'online.') > Microsoft Online Community Support > > ================================================== > For MSDN subscribers whose posts are left unanswered, please check this > document: http://blogs.msdn.com/msdnts/pages/postingAlias.aspx > > Get notification to my posts through email? Please refer to > http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif > 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 > http://msdn.microsoft.com/subscriptions/support/default.aspx. > ================================================== > This posting is provided "AS IS" with no warranties, and confers no rights. > > 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 dteFuture as date>> Dim errNum as Integer >> errNum = 1 dteFuture = dateadd("s",10,now) 'Try for 10 seconds, change at will<g>>> While errNum > 0 and dtefuture > Now Also, if you decide to use FSO, I suggest doing a Google search in this group>> FileCopy "server file", "client file" >> errNum = Err.Number >> Err.Clear >> Wend to see how this has worked for others. Many here advise against it since it brings its own set of issues. Regards, Saga -- Show quote"Peter Karlström" <HayRoller@newsgroup.nospam> wrote in message news:5E18A574-51A7-4DD5-8E2E-C39E710A59C9@microsoft.com... > 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 > Sweden > > > "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 >> Err.Clear >> Wend >> As long as the file is closed, the loop will stop and the file will be >> copied. >> >> Another approach is to use FileSystemObject >> (http://msdn2.microsoft.com/en-us/library/2z9ffy99.aspx) , 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. >> >> Sincerely, >> Jialiang Ge (jia***@online.microsoft.com, remove 'online.') >> Microsoft Online Community Support >> >> ================================================== >> For MSDN subscribers whose posts are left unanswered, please check this >> document: http://blogs.msdn.com/msdnts/pages/postingAlias.aspx >> >> Get notification to my posts through email? Please refer to >> http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif >> 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 >> http://msdn.microsoft.com/subscriptions/support/default.aspx. >> ================================================== >> This posting is provided "AS IS" with no warranties, and confers no rights. >> >> Given the indefinite duration of the lock, I'd also include a DoEvents in
the loop to keep the App responsive, and probably a very short Sleep call to make sure the App doesn't hog the machine Tony Proctor "Saga" <antiSpam@somewhere.com> wrote in message file, but Inews:u5cUSS8JIHA.5224@TK2MSFTNGP02.phx.gbl... > > 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 > would add a time loop so that you can define how long you want the code will<g>> 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 Show quote > http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif> >> While errNum > 0 and dtefuture > Now > >> FileCopy "server file", "client file" > >> errNum = Err.Number > >> Err.Clear > >> Wend > > 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. > > Regards, > Saga > -- > > > > "Peter Karlström" <HayRoller@newsgroup.nospam> wrote in message > news:5E18A574-51A7-4DD5-8E2E-C39E710A59C9@microsoft.com... > > 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 > > Sweden > > > > > > "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 > >> Err.Clear > >> Wend > >> As long as the file is closed, the loop will stop and the file will be > >> copied. > >> > >> Another approach is to use FileSystemObject > >> (http://msdn2.microsoft.com/en-us/library/2z9ffy99.aspx) , 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. > >> > >> Sincerely, > >> Jialiang Ge (jia***@online.microsoft.com, remove 'online.') > >> Microsoft Online Community Support > >> > >> ================================================== > >> For MSDN subscribers whose posts are left unanswered, please check this > >> document: http://blogs.msdn.com/msdnts/pages/postingAlias.aspx > >> > >> Get notification to my posts through email? Please refer to > >> Show quote > >> 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 > >> http://msdn.microsoft.com/subscriptions/support/default.aspx. > >> ================================================== > >> This posting is provided "AS IS" with no warranties, and confers no rights. > >> > >> > > "Saga" <antiSpam@somewhere.com> wrote in message As you are copying a list of files would it not be more efficient if a news:u5cUSS8JIHA.5224@TK2MSFTNGP02.phx.gbl... > > 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: problem is encountered with a file to mark it in the list then go on to the next file and then return to any problem files once all the easy ones are copied, that way you are still introducing a delay of sorts but the program is doing something useful rather than just hanging around. You would want to have the capability of using a loop delay like Jialang suggested, but only after every other attempt has failed and all the files that will copy are copied. Dave O. Jialiang Ge [MSFT] wrote:
> Hello Peter, <snip>> > 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. > > <snip>> Another approach is to use FileSystemObject > (http://msdn2.microsoft.com/en-us/library/2z9ffy99.aspx) , 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. > Your suggestion about using the FileSystemObject is quite interesting. Where did your read that it also can copy locked files? Personally I hate the FSO (as it contains some nasty bugs and it results in another - for me useless - dependency) but if you can put me in the right direction, I'll consider it again. Probably it's also possible using the Win32 API's (as FSO is a wrapper around it). I hope the source you've used to state this mentions it. Sinna Hello Sinna,
As you said, FileSystemObject.CopyFile internally calls the copy file Windows API. In this post, I admit that a direct call of Windows API is a better solution, to avoid the 'dependency' and the known issues of 'FileSystemObject'. Thank you, and thank all the other warm-hearted community members, Kev, Saga, for your great inputs of suggestions. Hello Peter, As other community members suggested, a direct call of file copy Windows API will be a better resolution for your request. Here is an example: ' declare the copyfile windows api Private Declare Function CopyFile Lib "kernel32" _ Alias "CopyFileA" (ByVal lpExistingFileName As String, _ ByVal lpNewFileName As String, ByVal bFailIfExists As Long) _ As Long ' wrap the windows api call Public Function APIFileCopy(src As String, dest As String, _ Optional FailIfDestExists As Boolean) As Boolean Dim lRet As Long lRet = CopyFile(src, dest, FailIfDestExists) APIFileCopy = (lRet > 0) End Function ' example of call dim bSuccess as boolean bSuccess = APIFileCopy ("C:\MyFile.txt", "D:\MyFile.txt") If you have any other question or concerns, please feel free to let us know. Regards, Jialiang Ge (jia***@online.microsoft.com, remove 'online.') Microsoft Online Community Support ================================================= When responding to posts, please "Reply to Group" via your newsreader so that others may learn and benefit from your issue. ================================================= This posting is provided "AS IS" with no warranties, and confers no rights. |
|||||||||||||||||||||||