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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Wait for file creating before continuing

Status
Not open for further replies.

pagey47

IS-IT--Management
Jun 7, 2004
32
GB
Hi there,
Ive got a program that exports a file then it using the winzip command line to zip it up, once zipped the program attaches the file to a email. The problem im having because the zipping is taking place externally from the program the email is trying to be sent when sometimes the zip file is not finished creating.

Is there any quick way i can get the program to loop around looking at the files last modified date then when its finished zipping carry on and exit out of the loop?

Thanks in advance
 
You could attempt to open the file exclusively in a try catch block within a loop. When successful you know that the file has been completly written and closed by WinZip.


Have a look at then Mode, Access and Share parameter of File.Open

Hope this helps.
 
Thank you for your response, could you put a couple of lines of code as a example im not quite sure what you mean.. thank you
 
Something like this
Code:
Dim bAbort As Boolean = False
While Not System.IO.File.Exists("c:\test\test.zip") And not bAbort
End While

I would also have a Cancel button which sets the bAbort variable true so that there is a way of exitling the loop if things go wrong.


Sweep
...if it works dont f*** with it
...if its f****ed blame someone else
...if its your fault that its f***ed, say and admit nothing.
 
Code:
Imports System
Imports System.IO

Code:
Dim path As String = "c:\temp\MyTest.txt"
Dim fs As FileStream
Dim ok as boolean = false
While Not ok
  Try
    fs = File.Open(path, FileMode.Open, FileAccess.Write, FileShare.None)
    ok = true
  Catch ex as Exception
    'you might want to test for an unexpected exception
  End Try
Loop
fs.Close

Typed NOT tested

Hope this helps.
 
Sweep, surely the file would "exist" before it had been completely written and closed?
 
earthandfire
Good point...tho Im not sure it does when creating zips.
Anyway your solution is better error trapped than the few lines I cobbled together.



Sweep
...if it works dont f*** with it
...if its f****ed blame someone else
...if its your fault that its f***ed, say and admit nothing.
 
Thank you earthandfire your solution was exactly what i was looking for, the problem i was having that i was checking if the file existed but this was not enough because it could exist but still be writing.
 
I had a similar problem some time ago with Access (using a solution like yours, Sweep), which is why I adopted this approach.

The solution, however came from here: thread713-1057619
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top