Solved

Start a .exe when a file appears in dir

Posted on 1999-01-24
8
147 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
 
LVL 7

Expert Comment

by:Inteqam
ID: 1470367
?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Link a textbox in excel to a cell with more than 255 characters. 5 66
Excel object stays open 19 70
vb6 connector to SQL Server 2 34
using web browser with BING 40 110
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

914 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now