Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

File.Copy problem - Empty file

Status
Not open for further replies.

modfather

MIS
Feb 15, 2000
75
US
Hi. I have a function that fires on the creation of a file in a directory. What I want to do is make two copies of the file, copying one to one directory on a remote machine and another copy to a directory on the local machine, and then I want to delete the original. Here's a small snippet of code:

System.IO.File.Copy(myFile, destFile1)
System.IO.File.Copy(myFile, destFile2)
IO.File.Delete(myFile)

"destFile1" is on the local machine, and "destFile2" is on the remote machine. The first copying works fine. But the 2nd copying results in a file that is sometimes blank - in other words, zero bytes. Is it possible that the Delete is firing before the completion of the Copying? If so, how do I avoid this. What I'm attempting right now is changing the order of "destFile1" and "destFile2", and changing the Copy on destFile1 to a "Move". Ideas?

Thanks very much for any brave enough to help me. Happy Holidays!
Steve
 
There are a few things you could try. First make sure that you network connection to the remote machine is solid. Is it possible that maybe the copy isn't happening because of a spotty connection?

It doesn't seem logical that the delete would fire before the copy was finished, but considering the random quarkyness of vb I wouldn't be terribly supprized if that was happening.

The easiest way to test against this is to just put a pause between each operation your perform.

I believe it is system.threading.sleep(milliseconds).

System.IO.File.Copy(myFile, destFile1)
system.threading.sleep(#ofmilliseconds).
System.IO.File.Copy(myFile, destFile2)
system.threading.sleep(#ofmilliseconds).
IO.File.Delete(myFile)




Let me know if this works. I have some other ideas if you still have problems.
 
intrex,

Thanks alot. The system.threading.thread.sleep method worked great. I had to fiddle with the number of ms, but 400 seems to work. Weird, huh? I noticed that it doesn't matter what order I do the copying in - the first destination can be zero bytes if the sleep isn't used. The second destination is always fine.
 
I think this might be happening because the calls you make to the system.IO.file class are creating one object and using it twice? If the first object isn't finished writing by the time you start the second call it won't know what to write.

you can probably do something like this to make your code much cleaner without the pauses.

dim myfile as new System.IO.File
dim myfile2 as new sytem.IO.File

myfile.copy(filename,destination)
myfile = nothing

myfile2.copy(filename,destination)
myfile2 = nothing

I don't have vs.net on the comp I am writting this from so I am sure the syntax is different than this, but you get the idea.
 
Well, this option seemed great, but it seems that the file is still in use by the first object whilst trying to copy the second. So now I get a message saying "The process cannot access the file 'c:\...' because it is being used by another process."

Grrr... I guess I'll have to go back to the timed event. Can I do a "move" across systems? If I can move it first, and then do a copy from the second, that'd be great.
 
try putting myfile.close between the copy call and the myfile=nothing statements. That might take care of the problem. I should have put that in the first post.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top