Solved

Start a .exe when a file appears in dir

Posted on 1999-01-24
8
150 Views
Last Modified: 2010-05-03
I need to write a little tsr that scans a certain directory.  When a file appears in that directory, it launches a program.  I know a little VB from my MS Access programming, but not of this nature.  Thanks.
0
Comment
Question by:qship
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 1

Accepted Solution

by:
sunj earned 70 total points
ID: 1470364
You can add a timer control to your form. Set the interval to be, say, 1000. Set the form to be invisible if you want. And add the following code:

Private Sub Timer1_Timer()
If Dir("c:\tmp\write.exe") <> "" Then
    Shell ("c:\tmp\write.exe")
    Unload Me
End If
End Sub

This will lauch the "write.exe" program if it appeared in a certain directory. After lauching the application, the program itself will terminate.
0
 
LVL 2

Expert Comment

by:dm_14
ID: 1470365
sunj's program will work but your CPU usage will be high. A better approach would be to you the  FindFirstChangeNotification API.

Code available if you want it.
0
 
LVL 4

Expert Comment

by:mcix
ID: 1470366
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Expert Comment

by:Inteqam
ID: 1470367
?
0
 

Author Comment

by:qship
ID: 1470368
Sunj:  I assume then, if I want the TSR to remain running 24/7 I would just not use the 'Unload Me' ??

dm_14:  I looked at the site provided by mcix (thank you, btw) but as I am not to up on VB, it was a little overwhelming.  Maybe you could provide just the snippet of code that would do what I am looking to do???
0
 

Author Comment

by:qship
ID: 1470369
Sunj:  I assume then, if I want the TSR to remain running 24/7 I would just not use the 'Unload Me' ??

dm_14:  I looked at the site provided by mcix (thank you, btw) but as I am not to up on VB, it was a little overwhelming.  Maybe you could provide just the snippet of code that would do what I am looking to do???
0
 
LVL 1

Expert Comment

by:sunj
ID: 1470370
yes, qship. You can just take away the Unload Me statement if you want your program remain running.
0
 
LVL 2

Expert Comment

by:dm_14
ID: 1470371
Try this

(1) Start a new project
(2) in you .bas module include the following :

Public Const INFINITE = &HFFFF
Public Const FILE_NOTIFY_CHANGE_FILE_NAME As Long = &H1
Public Const FILE_NOTIFY_CHANGE_DIR_NAME As Long = &H2
Public Const FILE_NOTIFY_CHANGE_ATTRIBUTES As Long = &H4
Public Const FILE_NOTIFY_CHANGE_SIZE As Long = &H8
Public Const FILE_NOTIFY_CHANGE_LAST_WRITE As Long = &H10
Public Const FILE_NOTIFY_CHANGE_LAST_ACCESS As Long = &H20
Public Const FILE_NOTIFY_CHANGE_CREATION As Long = &H40
Public Const FILE_NOTIFY_CHANGE_SECURITY As Long = &H100
Public Const FILE_NOTIFY_FLAGS = FILE_NOTIFY_CHANGE_ATTRIBUTES Or _
                                 FILE_NOTIFY_CHANGE_FILE_NAME Or _
                                 FILE_NOTIFY_CHANGE_LAST_WRITE

Public Declare Function FindFirstChangeNotification Lib "kernel32" _
    Alias "FindFirstChangeNotificationA" _
   (ByVal lpPathName As String, _
    ByVal bWatchSubtree As Long, _
    ByVal dwNotifyFilter As Long) As Long

Public Declare Function FindCloseChangeNotification Lib "kernel32" _
   (ByVal hChangeHandle As Long) As Long

Public Declare Function FindNextChangeNotification Lib "kernel32" _
   (ByVal hChangeHandle As Long) As Long

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

Public Const WAIT_OBJECT_0 = &H0
Public Const WAIT_ABANDONED = &H80
Public Const WAIT_IO_COMPLETION = &HC0
Public Const WAIT_TIMEOUT = &H102
Public Const STATUS_PENDING = &H103

(3) in Form1 and a comman1 button and in its click event place the following code :

    Dim r As Long, sPath As String, lStatus As Long
    Dim lDir As Long, lFound1 As Long, lFound2 As Long
   
    sSourceDir = "C:\temp1\"
    sPath = Left$(sSourceDir, Len(sSourceDir) - 1)
    lDir = FindFirstChangeNotification(sPath, False, FILE_NOTIFY_FLAGS)
    Do
      lFound1 = WaitForSingleObject(lDir, 100)
      DoEvents: If bFlag = True Then Exit Do
    Loop While lFound1 <> 0
    Do
     lFound2 = FindNextChangeNotification(lDir)
     Do
      lFound2 = WaitForSingleObject(lDir, 100)
      DoEvents
     Loop While lFound2 <> 0
     
     If iFound2 = 0 Then
        DoEvents
        sApp = Dir(sSourceDir)
        If sApp<>"" then
            Shell (sSourceDir & sApp)
            sApp = ""
        End If
       
     End If
   Loop
   lFound2 = FindCloseChangeNotification(lDir)

Change sSourceDir to the directory you want to check and then run the project and click on the command button. Whenever a file is create in the directory it will execute that file.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question