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

Drop-Box function: Wait state until file shows up in a directory

Status
Not open for further replies.

jbs

Programmer
Feb 19, 2000
121
US
Help? I'm still sort of a new VBasic programmer and the problem described below is a bit beyond my current experience. I suspect I need us use a win32 function call or some type of libary function which I've never had to use before.

I need to write a section of a VBasic program that sits and waits for a file to show-up in a directory. After the file shows up in the directory the program continues on its way through the rest of the program.

My first attempt at this function made use of the Scripting.FileSystemObject. I set up a loop which constantly checked to see if the file was there...if not it went and checked for the file again, and again, and again. Clearly this brought the entire computer to it's knees while waiting for the file to showup.

While looking through various books I came saw that their is a "FindFirstChangeNotification Lib "Kernel32" Alias "FindFirstChangeNotificationA" API function call that is somehow available, however I could not find some real useful sample code.

I could arrange for the file to be the only file that would ever show up in a directory and then look for a change in the directory structure. Regardless, could someone help me here? this will be my first API that I've create using what sounds like a Win32 Library call.....

Anyway here is a summary of what I need:

Directory: c:\test
File that I'm looking for: c:\test\test.txt

Functions should wait for test.txt to show up in the directory c:\test or timeout after 60 seconds.

Other windows applications and vital system functions should be able to continue without having degraded performance while the vbasic program 'sleeps' or 'waits' until the file arrives in the directory.

help??

thanks,

jerry

 
Hi,
Try the FindFirstFile and Sleep API...

Example:
This code must be put in a MODULE:
Public Declare Function FindFirstFile Lib "kernel32.dll" Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
Public Declare Sub Sleep Lib "kernel32.dll" (ByVal dwMilliseconds As Long)

Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type

Type WIN32_FIND_DATA
dwFileAttributes As Long
ftCreationTime As FILETIME
ftLastAccessTime As FILETIME
ftLastWriteTime As FILETIME
nFileSizeHigh As Long
nFileSizeLow As Long
dwReserved0 As Long
dwReserved1 As Long
cFileName As String * 260
cAlternate As String * 14
End Type

Public hsearch As Long
Public findinfo As WIN32_FIND_DATA

Put this code in your FORM/MODULE:
Private Sub Command1_Click()
Dim x
start:
hsearch = FindFirstFile("C:\test\test.txt", findinfo)
If (hsearch = -1) Then
' no files match the search string
Debug.Print "(no files matched search parameter)"
Sleep 60000 ' 60000 milliseconds = 60 seconds to delay
x = x + 1
If (x > 3) Then ' Use this as a timeout feature
End
End If
GoTo start
End If
'continue processing code here...
End Sub


I hope this helps!
Brett Please visit my websites!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top