|
-
May 1st, 2019, 04:34 PM
#1
file save completed
i have a file saved from an external program, i have to open and read, i need to know when the writing of that file is completed
previously i have been using a simple loop
Code:
Do While FileLen(myfile) = 0
DoEvents
l = l + 1
If l > 10000 Then MsgBox inv & " timed out": Exit Sub
Loop
this has been working for several years, but on a new, faster machine, the code continues before the file saving has completed, i can obviously extend the loop or use a sleep, but i would like some way to actually test, when the code should continue
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
May 1st, 2019, 04:49 PM
#2
Re: file save completed
What type of file is this?
One possible idea:
1. Check to see if the file exists. If no, not created yet, else if yes, then go to step 2.
2. Get the length/size of the file. Wait 1-3 seconds, then check the size/length again. If it is not the same, file is still being built, wait again and re-check.
This is assuming that the file is not locked/unavailable while it is being created.
-
May 1st, 2019, 08:12 PM
#3
Re: file save completed
 Originally Posted by westconn1
... i need to know when the writing of that file is completed
You might be able to detect that via ReadDirectoryChangesW. This thread has some examples of that API and the accepted answer is actually an example you posted yourself.
-
May 2nd, 2019, 02:48 AM
#4
Re: file save completed
This can be a solution: use a loop to see if the file is exclusively accessible
1. Open file with Lock Write
2. if it fails, repeat
3. if it does not fail, continue
-
May 2nd, 2019, 03:36 AM
#5
Re: file save completed
You might be able to detect that via ReadDirectoryChangesW. This thread has some examples of that API and the accepted answer is actually an example you posted yourself.
that is how i know the file is created, but the file exists even before writing is complete
2. Get the length/size of the file. Wait 1-3 seconds, then check the size/length again. If it is not the same, file is still being built, wait again and re-check.
a 3 second wait should be more than adequate, but i was trying to avoid such a long delay unless required, many of the files are quite small, but this appears to have determined the cause of the problem, the files written on the new machine are bigger than previous systems, more than 10x the size
This can be a solution: use a loop to see if the file is exclusively accessible
this would probably be the best solution for the current situation, but i was not keen to implement it
i will have to look at the file size and reconsider what utilities i should use, i just assumed that as windows 10 had a built in PDF printer driver i should just use it, but it looks like i should change to using the free one i was using previously
20Kb against >300Kb
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
-
May 2nd, 2019, 04:38 AM
#6
Re: file save completed
 Originally Posted by westconn1
that is how i know the file is created, but the file exists even before writing is complete
According to my tests, applications (or perhaps the OS?) updates the last modified timestamp of a file when they are done writing and have closed the file. ReadDirectoryChangesW can detect when that happens if you specify FILE_NOTIFY_CHANGE_LAST_WRITE for the dwNotifyFilter argument. You could also specify FILE_NOTIFY_CHANGE_SIZE if the size of the file is guaranteed to change after it's modified.
-
May 2nd, 2019, 05:34 AM
#7
Re: file save completed
hi,
here a function to check if the File is open, it will return True or False if open
Code:
Public Function FileIsOpened(Filename As String) As Boolean
'prüft ob eine Datei (von einem anderen Programm) geöffnet ist
Dim FNr As Integer
If Not ExistFile(Filename) Then
Exit Function
End If
On Error GoTo Fehler
FNr = FreeFile
Open Filename For Input Lock Read Write As #FNr
Close #FNr
Exit Function
Fehler:
FileIsOpened = True
End Function
Public Function ExistFile(Filename As String, _
Optional Hidden As Boolean = False) _
As Boolean
'prüft die Existenz einer Datei
If Hidden Then
If Len(Dir(Filename, vbHidden)) > 0 Then
ExistFile = True
End If
Else
If Len(Dir(Filename)) > 0 Then
ExistFile = True
End If
End If
End Function
more or less what Gibra had in mind
to hunt a species to extinction is not logical !
since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.
-
May 2nd, 2019, 04:11 PM
#8
Re: file save completed
ReadDirectoryChangesW can detect when that happens if you specify FILE_NOTIFY_CHANGE_LAST_WRITE for the dwNotifyFilter argument.
i have been using FILE_ACTION_ADDED, which returns as soon as the file starts to write, i will have to look at changing to FILE_NOTIFY_CHANGE_LAST_WRITE, although i never had a problem in the past with a different pdf printer driver
here a function to check if the File is open, it will return True or False if open
thanks for the function, i had been considering using something like that, but probably inline, rather than a separate function
i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next
dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part
come back and mark your original post as resolved if your problem is fixed
pete
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
|