Copying/Moving files only after full write.

Posted on 1999-06-24
Medium Priority
Last Modified: 2010-05-02
Hello all.  Please help me with this.  I need a quick solution to copying/moving files within VB 5.0 but ONLY after it has fully been written to the directory.  I currently have an application that uses an infinite loop to search several directories for files and there is no problem with moving a file into them since it really just moves an OS pointer, but copying a large file does cause problem.  Is there a relatively easy solution to this problem in VB 5.0, say... a function to retrieve status, then pass that to a copy function, or does it require full api writing, include wait/change notifications?

Please help.

thank you.  
Question by:tryinhard
LVL 12

Expert Comment

ID: 1519607
have your loop try to RENAME the file. It'll fail with an ERROR until the file is completed being written.



Author Comment

ID: 1519608
sorry, but the point is to not get any errors......I need first to copy the file, then to move it.   I really need to have access to knowledge of when the file is completely written and a method for operations then.

Expert Comment

ID: 1519609
before the file is completly weritten, you can see it's name in the directory, but the size will be zero length. maybe you can wait until the file appears in the directory, and then wait for it's size to be larger then 0.
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.


Accepted Solution

s_lavie earned 200 total points
ID: 1519610
I guess mark2150 had a point.
Use a function that checks if a file is copied:

Private Function isFileExists(fullPath as string) as Boolean

isFileExists = False

On Error Resume Next
Open fullPath for input as #1
If Err.Number = 53 then Exit Function ' file not found
Close #1

isFileExists = True

End Function

Now call to isFileExists within a loop until it is True.
LVL 12

Expert Comment

ID: 1519611
You set an ERROR TRAP that handles the *EXPECTED* error - the operator never sees it.

' You get here when you've got file!

RESUME   'Retry line that failed

Simple enough, no?



Author Comment

ID: 1519612
yes, yes...that solves it.  I see now what mark was saying, it just wasn't said clearly, and in this case re-naming the file is not an option, though what he meant is essentially the same idea.

s_lavie's comments were what triggered my understanding, so do accept the points and my thanks... I just learned an important technique!!

LVL 12

Expert Comment

ID: 1519613
The one advantage to the rename process is that a rename operator is 'atomic'. That is to say if two rename requests hit the server at once, only one will be honored. It'll either work 100% or fail, no half measures. If you're in a situation where you have *several* machines running on a net and you want to make sure that one and only one processes the file, then renaming to a unique name (perhaps a randomly generated one) allows you to *verify* that you got the file. If the target file name is present, then *YOUR* thread and *NO OTHER* has the file. This also has the advantage of working independent of the OS of the computer or the LAN. You can have a mix of DOS/Win 3.1/Win 95/NT stations running on NT/Novell/Banyon Vines, etc. LAN and it will *STILL* work correctly.



Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

587 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question