|
-
Oct 6th, 2007, 08:03 AM
#1
Thread Starter
Fanatic Member
[RESOLVED] Using a file immediately after replacing it.
I’m having a problem with copying files and then immediately using them. It doesn’t seem like to code allows enough time for the file to be copied. Is there a way to pause the code until the file has been copied and then continue? I don’t want a noticeable delay in the code execution so I don’t really want a timer to wait for 1 second then continue.
-
Oct 6th, 2007, 08:15 AM
#2
Addicted Member
Re: Using a file immediately after replacing it.
Try using the sleep API, or the line DoEvents.
It should do all the events it's currently doing to save the time CPU.
 Originally Posted by MSDN
DoEvents passes control to the operating system. Control is returned after the operating system has finished processing the events in its queue and all keys in the SendKeys queue have been sent.
 Originally Posted by Vanasha
Sorry, i'm slow.
-
Oct 7th, 2007, 01:03 AM
#3
Addicted Member
Re: Using a file immediately after replacing it.
Just a suggestion...
Before the copying begins, store the filesize into a variable then run a timer for every 5 sec or so to check if the filesize originally matches the one thats copying/copied. Once it matches, then have it execute the file.
-
Oct 7th, 2007, 02:08 AM
#4
Re: Using a file immediately after replacing it.
I deal with this everyday. This is my solution:
Code:
FileCopy SourceFile, DestFile
On Error Resume Next
Do
DoEvents
Open DestFile for Input As #1
If Err = 0 then
Close #1
Exit Do
End If
Err.Clear
Loop
On Error Goto 0 '-- or replace with your Error handling
-
Oct 7th, 2007, 03:42 AM
#5
Re: Using a file immediately after replacing it.
 Originally Posted by TURKINATOR
... Before the copying begins, store the filesize into a variable then run a timer for every 5 sec or so to check if the filesize originally matches the one thats copying/copied. Once it matches, then have it execute the file...
It's a good idea, but can go terrible wrong when filesizes equal. It's an endless process then.
-
Oct 9th, 2007, 05:37 AM
#6
Thread Starter
Fanatic Member
Re: Using a file immediately after replacing it.
Thanks all.
I'm still having problems though. I tried using anhn's code but I need to delete the destination file first as if it exists, no error is generated.
Then I had problems with deleting it and making sure it was deleted before I copied the file so I added the following. Can anyone see what is wrong with it or suggest another method?
vb Code:
Sourcefile = .FileName
Destinationfile = App.Path & "\Undefined.ini"
Kill Destinationfile
On Error Resume Next
Do
DoEvents
Open Destinationfile For Input As #1
If Err <> 0 Then
Close #1
Exit Do
End If
Loop
Err.Clear
FileCopy Sourcefile, Destinationfile
'Make sure File has been copied.
Do
DoEvents
Open Destinationfile For Input As #1
If Err = 0 Then
Close #1
Exit Do
End If
Err.Clear
Loop
'Revert to normal error handler
On Error GoTo Openfile_Error
-
Oct 9th, 2007, 05:51 AM
#7
Re: Using a file immediately after replacing it.
While do you use the Do..Loop twice. Not make sense!
Code:
Sourcefile = .FileName
Destinationfile = App.Path & "\Undefined.ini"
If Dir(Destinationfile) <> "" then
Kill Destinationfile
End If
FileCopy Sourcefile, Destinationfile
'Make sure File has been copied.
On Error Resume Next
Do
DoEvents
Open Destinationfile For Input As #1
If Err = 0 Then
Close #1
Exit Do
End If
Err.Clear
Loop
'Revert to normal error handler
On Error GoTo Openfile_Error
-
Oct 9th, 2007, 05:57 AM
#8
Thread Starter
Fanatic Member
Re: Using a file immediately after replacing it.
Hi anhn.
I don't know if it's my computer but it seems to be the same situation with killing a file as copying a file.
Killing it seems to take time and I need to make sure it's dead before I continue on with the copy. Hence the Do..Loop.
With the first Do..Loop I keep trying to open the file until an error is generated.
-
Oct 9th, 2007, 06:05 AM
#9
Re: Using a file immediately after replacing it.
Unless the Destinationfile is being used, you can Kill it right before you copying.
If you want a wait, do it like this:
Code:
If Dir(Destinationfile) <> "" then
Kill Destinationfile
Do
DoEvents
Loop Until Dir(Destinationfile) = ""
End If
-
Oct 9th, 2007, 06:16 AM
#10
Thread Starter
Fanatic Member
Re: Using a file immediately after replacing it.
-
Oct 9th, 2007, 06:26 AM
#11
Re: Using a file immediately after replacing it.
Just rename the file rather than deleting it. Delete renamed file later.
And your loop is adding delay to the file copy... it's grabbing CPU time again and again rather than giving most of it to the OS. The description of DoEvents is misleading, its only for processing other messages queued in your apps thread (and not the file copy) if I'm not mistaken.
As suggested earlier use Sleep to pause the loop and after pause check if file can be accessed.
Last edited by leinad31; Oct 9th, 2007 at 06:44 AM.
-
Oct 9th, 2007, 06:30 AM
#12
Re: Using a file immediately after replacing it.
Well, this is how I always do it:
1) Kill the original file.
2) Write your file with temporary name (GetTempFileName). (Introduces a delay)
3) Using a timer with a few milliseconds delay, check for the original file. When it no longer exists :-
4) Rename the temp file.
-
Oct 9th, 2007, 07:54 AM
#13
Thread Starter
Fanatic Member
Re: Using a file immediately after replacing it.
I need to retain the original. Can you show me how to implement the sleep method? This is a timer? I've never used one.
How long do I sleep for? Sleep 100? I guess the smaller I make the number the harder the CPU works.
With this declaration :
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
vb Code:
If Dir(Destinationfile) <> "" Then
Kill Destinationfile
Do
Sleep 100
Loop Until Dir(Destinationfile) = ""
End If
It says that Sleep isn't defined. Is there something else I need to do?
-
Oct 9th, 2007, 08:02 AM
#14
Re: Using a file immediately after replacing it.
Code:
Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Private Sub cmdPause_Click()
Sleep 1000
'In this case, the 1000 milliseconds puts the application to bed for 1 second.
End Sub
-
Oct 9th, 2007, 10:12 AM
#15
Thread Starter
Fanatic Member
Re: Using a file immediately after replacing it.
Finally working.
Thanks all. I used this in the end. The file sizes I am copying are tiny hence the Sleep 0.5.
Code:
'Kill Destination file
If Dir(Destinationfile) <> "" Then
Kill Destinationfile
Do
Sleep 0.5
Loop Until Dir(Destinationfile) = ""
End If
FileCopy Sourcefile, Destinationfile
'Make sure File has been copied.
On Error Resume Next
Do
Sleep 0.5
Open Destinationfile For Input As #1
If Err = 0 Then
Close #1
Exit Do
End If
Err.Clear
Loop
'Revert to normal error handler
On Error GoTo Save_Error
-
Oct 9th, 2007, 10:34 AM
#16
Re: Using a file immediately after replacing it.
If I were you, I would put a comment in here right above Sleep
Code:
Do
Sleep 0.5
Loop Until Dir(Destinationfile) = ""
6 or 8 months from now, you may be looking at this code and wondering why in the world you used the Sleep API.
Someone else could take the program over from you, look at this code, and wonder the same thing.
If you consider this resolved, you could help us out by pulling down the Thread Tools menu and clicking the Mark Thread Resolved menu item. That will let everyone know that you have your answer.
Thank you.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|