Solved

Start a .exe when a file appears in dir

Posted on 1999-01-24
8
152 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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

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!

Question has a verified solution.

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

Suggested Solutions

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…

710 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