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

File monitoring

Status
Not open for further replies.

sdh

Programmer
Joined
Apr 30, 2001
Messages
121
Location
GB
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
 
Would it be possible to just keep an eye on the size of the file and when it stops changing, to decide that it is finished? I generally use the FileSystemObject for this type of stuff.

Incidentally, are you by any chance writing an app that deals with Scientific Data? If so, I'd like to get in touch.

Mark
 
The checking filesize is what the code in red does but it cannot tell me when the file has actually been released.
Do you know a way other than this
In response to your question yes I work within the scientific community and always write progs for scientific data.
 
Sorry - I'm an idiot :) - I didn't read your post properly
I don't see anything in the FileSystemObject library which intrinsically detects whether a file is still being written to, so my guess would be to try to read the file and see if an error is returned as below (I've not tried this)

Private Function blnFileReleased(fil As File) As Boolean
Dim fsoFSO As FileSystemObject, tsTxt As TextStream
Set fsoFSO = New FileSystemObject

On Error Resume Next
Set tsTxt = fsoFSO.OpenTextFile(fil.Path, ForReading)

If Err.Number <> 0 Then
blnFileReleased = True
Else
blnFileReleased = False
End If

End Function

Which application is creating the file you want to monitor?
Some applications are helpful in that they set flag files or status files to let you know what they are doing.


THanks
Mark
 
Thanks very much I will give it a try.
the file is being wriiten by a recital program in a unix environment and I am then trying to convert to pdf once it has been placed in a certain folder.
In an answer to your thread on PDF's are you using the reader or the full blown acrobat?
The automation capabilites for each are different.
Adobe have an sdk on thier website which should help what are you specifically trying to do?

thanks again
sdh
 
The following function may be of some use:
[tt]
Public Function IsFileOpen(strFile As String) As Boolean
Dim fso As FileSystemObject
Dim TestFile As TextStream

IsFileOpen = False
Set fso = New FileSystemObject
If fso.FileExists(strFile) Then
On Error GoTo CannotAccess
Set TestFile = fso.OpenTextFile(strFile, ForAppending)
On Error GoTo 0
End If
Exit Function
CannotAccess:
If Err.Number = 70 Then
IsFileOpen = True
Resume Next
End If
End Function
 
Another method would be to get your unix process to output an additional file once it has written the PDF file. Your application checks for the additional file and when it is found it, you can process the PDF file.

eg. unix process outputs MyFilename.done, then you know to process the file Myfilename.PDF.

The problem with checking the filesize is that often when doing filecopying / FTP etc. The file can be created as the full file size before any data has actually been written to it.

Hope this helps,

Chris Dukes
 
Rather than looping continuously through the folder and checking file sizes, look at the following APIs -

1) FindFirstChangeNotification
(Used with the FILE_NOTIFY_CHANGE_LAST_WRITE attribute)

2) WaitForSingleObject
(Used with the handle returned by FindFirstChangeNotification)

3) FindCloseChangeNotification
(Used to close the handle opened by FindFirstChangeNotification)

It is a little more complicated, but it will produce the most efficient method. - Jeff Marler B-)
 
To one and all.

Thanks for all your help, I will try out these methods
 
Problem with file delivery even with size check is network lag or less frequent file system lag.

Best to create a temp file for the delivery stage.
Once file is delivered rename it.


Wil Mead
wmead@optonline.net

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top