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

Search for a file - visual basic

Status
Not open for further replies.

dkwaters

Programmer
Feb 16, 2003
17
0
0
GB
Hi,

I have a VB app that connects to a FTP site and continually looks for files placed on the site. I am currently using a loop that continually polls the site and when the file exists grabs it. However this is really resource intensive, is there a way I can start my program but have say a function that is called only when there is a change to a specified folder, without having this loop?

thanks
 
I've made a "folder watcher" using APIs that would monitor folder(s) for changes (actually I think I got most of the code from some web site)...you'll notice it uses a Timer control--not all that efficient, but much less of a performance hit than a Do...While loop.

Hope this is useful.

Below is a sample (check the MSDN for more information on the APIs)

'!! ================================================================================================
'-- Place the following code in a form which has a Timer control named "tmrCheckFolder"
'!! ================================================================================================
Option Explicit

Private Enum FILE_NOTIFY_CHANGE
FNC_FILE_NAME = &H1
FNC_DIR_NAME = &H2
' FNC_ATTRIBUTES = &H4
' FNC_SIZE = &H8
' FNC_LAST_WRITE = &H10
' FNC_SECURITY = &H100
End Enum

Private Declare Function FindFirstChangeNotification Lib "kernel32" Alias "FindFirstChangeNotificationA" (ByVal lpPathName As String, ByVal bWatchSubtree As Long, ByVal dwNotifyFilter As Byte) As Long
Private Declare Function FindNextChangeNotification Lib "kernel32" (ByVal hChangeHandle As Long) As Long
Private Declare Function FindCloseChangeNotification Lib "kernel32" (ByVal hChangeHandle As Long) As Long

Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long

Private Declare Function GetLastError Lib "kernel32" () As Long

Private mlngHandle As Long
Private mstrFolder As String

Private Const WAIT_OBJECT_0 As Long = 0
Private Const INVALID_HANDLE_VALUE As Long = -1
'

Private Sub Form_Load()
'===================================================================================================
mstrFolder = "C:\Temp"

'-- When this is true, changes in subdirectories will also trigger "WaitForSingleObject"
Const cblnCHECK_RECURSIVELY As Boolean = False

'!! I know you won't believe me, but without this Debug.Print statement it wouldn't work in a compiled version.
Debug.Print "Watching folder: " & mstrFolder & IIf(cblnCHECK_RECURSIVELY, vbCrLf & vbCrLf & "Recursively", vbNullString)

mlngHandle = FindFirstChangeNotification(mstrFolder, cblnCHECK_RECURSIVELY, FNC_FILE_NAME Or FNC_DIR_NAME)

If mlngHandle = -1 Then
MsgBox "Failed on: " & mstrFolder & vbCrLf & vbCrLf & "Error number: " & GetLastError(), vbExclamation
Unload Me
Else
With Me.tmrCheckFolder
.Interval = 250
.Enabled = True
End With
End If
End Sub

Private Sub Form_Unload(Cancel As Integer)
'===================================================================================================
If mlngHandle <> INVALID_HANDLE_VALUE Then Call FindCloseChangeNotification(mlngHandle)
End Sub

Private Sub tmrCheckFolder_Timer()
'===================================================================================================
Call CheckFolder
End Sub

Private Sub CheckFolder()
'===================================================================================================
If WaitForSingleObject(mlngHandle, 100) = WAIT_OBJECT_0 Then
Debug.Print "Change occurred in: " & mstrFolder
Call FindNextChangeNotification(mlngHandle)
End If
End Sub
 
Sadly the Notification events won't work against a 3rd party FTP site...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top