In an app I am writing, I watch a folder to see if files are placed there if so they are processed. The problem I have my app using the code below tells me when the file is placed in the directory but not when it has finished writing the file. This is the information I need to know for it to be correctly processed can someone tell me how I can modify the code to alert me when the file has been closed after being written. (see my attempts in red using the sleep api)
I would be very grateful.
Sub ScanFiles(folderspec)
Dim fs, f, f1, fc, s, PDFFIleObject, PDFName As String
Dim a As String, i, FileSize, RptChanged As Boolean
Set fs = CreateObject("Scripting.FileSystemObject"
Set f = fs.GetFolder(folderspec)
Set fc = f.Files
For Each f1 In fc
If Right(f1.Name, 4) = ".rpt" Then
'A report script file - find the correspondind PDF (if any)
PDFName = folderspec & Left(f1.Name, Len(f1.Name) - 4) & ".pdf"
a = Dir(PDFName)
RptChanged = False
If Len(a) <> 0 Then
'PDF version exists, check date
Set PDFFIleObject = fs.GetFile(PDFName)
If f1.DateLastModified > PDFFIleObject.DateLastModified Then
'The RPT is more recent than the PDF file
RptChanged = True
End If
Else
'no PDF file at all
RptChanged = True
End If
[/blue]
If RptChanged Then
FileSize = f1.Size
'wait a bit then check file size again
Sleep 10000
If FileSize = f1.Size Then
'no change in file size - OK to convert
CreatePDF PDFName
PDFcreate = True
updatelocalDB
' append information to logfile
logreport (Text1.text)
f1.Delete[/red]
End If
End If
End If
DoEvents
Next
End Sub
I would be very grateful.
Sub ScanFiles(folderspec)
Dim fs, f, f1, fc, s, PDFFIleObject, PDFName As String
Dim a As String, i, FileSize, RptChanged As Boolean
Set fs = CreateObject("Scripting.FileSystemObject"

Set f = fs.GetFolder(folderspec)
Set fc = f.Files
For Each f1 In fc
If Right(f1.Name, 4) = ".rpt" Then
'A report script file - find the correspondind PDF (if any)
PDFName = folderspec & Left(f1.Name, Len(f1.Name) - 4) & ".pdf"
a = Dir(PDFName)
RptChanged = False
If Len(a) <> 0 Then
'PDF version exists, check date
Set PDFFIleObject = fs.GetFile(PDFName)
If f1.DateLastModified > PDFFIleObject.DateLastModified Then
'The RPT is more recent than the PDF file
RptChanged = True
End If
Else
'no PDF file at all
RptChanged = True
End If
[/blue]
If RptChanged Then
FileSize = f1.Size
'wait a bit then check file size again
Sleep 10000
If FileSize = f1.Size Then
'no change in file size - OK to convert
CreatePDF PDFName
PDFcreate = True
updatelocalDB
' append information to logfile
logreport (Text1.text)
f1.Delete[/red]
End If
End If
End If
DoEvents
Next
End Sub