Results 1 to 16 of 16

Thread: [RESOLVED] Using a file immediately after replacing it.

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2006
    Location
    London, UK
    Posts
    817

    Resolved [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.

  2. #2
    Addicted Member Vanasha's Avatar
    Join Date
    Jun 2007
    Location
    In a bin.>_>
    Posts
    152

    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.
    Quote 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.
    Quote Originally Posted by Vanasha
    Sorry, i'm slow.


  3. #3
    Addicted Member
    Join Date
    Dec 2006
    Posts
    207

    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.

  4. #4
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    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

  5. #5
    PowerPoster gavio's Avatar
    Join Date
    Feb 2006
    Location
    GMT+1
    Posts
    4,462

    Re: Using a file immediately after replacing it.

    Quote 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.

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2006
    Location
    London, UK
    Posts
    817

    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:
    1. Sourcefile = .FileName
    2. Destinationfile = App.Path & "\Undefined.ini"
    3. Kill Destinationfile
    4. On Error Resume Next
    5. Do
    6.    DoEvents
    7.    Open Destinationfile For Input As #1
    8.    If Err <> 0 Then
    9.         Close #1
    10.       Exit Do
    11.     End If
    12. Loop
    13. Err.Clear
    14. FileCopy Sourcefile, Destinationfile
    15. 'Make sure File has been copied.
    16. Do
    17.    DoEvents
    18.    Open Destinationfile For Input As #1
    19.    If Err = 0 Then
    20.       Close #1
    21.       Exit Do
    22.    End If
    23.    Err.Clear
    24. Loop
    25. 'Revert to normal error handler
    26. On Error GoTo Openfile_Error

  7. #7
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    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

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2006
    Location
    London, UK
    Posts
    817

    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.

  9. #9
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    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

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2006
    Location
    London, UK
    Posts
    817

    Re: Using a file immediately after replacing it.

    It's still not working.

    If I put a breakpoint on Kill Destinationfile it works.
    If I put a breakpoint on FileCopy Sourcefile, Destinationfile it doesn't break (or pause) at that line. What could be causing this line to be skipped?

    Code:
                If Dir(Destinationfile) <> "" Then
                   Kill Destinationfile
                   Do
                      DoEvents
                   Loop Until Dir(Destinationfile) = ""
                End If
                FileCopy Sourcefile, Destinationfile


  11. #11
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    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.

  12. #12
    Fanatic Member schoolbusdriver's Avatar
    Join Date
    Jan 2006
    Location
    O'er yonder
    Posts
    1,020

    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.

  13. #13

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2006
    Location
    London, UK
    Posts
    817

    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:
    1. If Dir(Destinationfile) <> "" Then
    2.                Kill Destinationfile
    3.                Do
    4.                   Sleep 100
    5.                Loop Until Dir(Destinationfile) = ""
    6.             End If

    It says that Sleep isn't defined. Is there something else I need to do?

  14. #14
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    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

  15. #15

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2006
    Location
    London, UK
    Posts
    817

    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

  16. #16
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    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
  •  



Click Here to Expand Forum to Full Width